Render a Form from an XSLT file - asp.net-mvc

I've generated the following XSLT file, and have created a Form that will post to an ASP.Net MVC action called Home/ProcessRequest:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<xsl:value-of select="Employee/Name"/>
<br />
<xsl:value-of select="Employee/ID"/>
<form method="post" action="/Home/ProcessRequest?id=42">
<input id="Action" name="Action" type="radio" value="Approved"></input> Approved <br />
<input id="Action" name="Action" type="radio" value="Rejected"></input> Rejected <br />
<input type="submit" value="Submit"></input>
</form>
</body>
</html>
Here is my XML File:
<Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Russ</Name>
<ID>42</ID>
</Employee>
This works fine the way it is, but I need to change the id parameter in my from from a hard coded integer, to use the ID element from my XML file. Does anyone know how to do this?

This should do it:
<form method="post" action="/Home/ProcessRequest?id={Employee/ID}">
{} is shorthand for using XPath inside attributes.

Related

XML to XML , unable to use copy of select with Repeated element

We need to perform XML to XML transformation, in expected output we need to have exact same input XML , but will need store WE value in variable , so that we can apply EDM and replace the value in the same place.
Please guide on the same.
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-
prefixes="#all" >
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:copy-of select="bap/tables/table"/>
</xsl:template>
</xsl:stylesheet>
INPUT -
<?xml version="1.0" encoding="UTF-8"?>
<bap name="ABC">
<tables>
<table name="IT">
<row id="0">
<field name="FIL">2019</field>
<field name="WE">000A</field>
<field name="CON" />
</row>
<row id="1">
<field name="FIL">2019</field>
<field name="WE">000B</field>
<field name="CONTROL" />
</row>
<row id="2">
<field name="FIL">2019</field>
<field name="WE">000C</field>
<field name="CON" />
</row>
</table>
</tables>
</bap>
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<bap name="ABC">
<tables>
<table name="IT">
<row id="0">
<field name="FIL">2019</field>
<field name="WE">0987</field> - value should be replaceable
<field name="CON" />
</row>
<row id="1">
<field name="FIL">2019</field>
<field name="WE">98I8</field>
<field name="CONTROL" />
</row>
<row id="2">
<field name="FIL">2019</field>
<field name="WE">76y5</field>
<field name="CON" />
</row>
</table>
</tables>

JSF components don't render when using JBoss6 runtime instead of Tomcat7

I have a simple form page as shown below:
<f:view contentType="text/html">
<h:head>
<title>Login Page</title>
</h:head>
<body>
<h1>Java Learning Center</h1><hr id="horizontalLine"/>
<h2><a>Account Login</a></h2>
<h:outputText value="#{userBean.errorMessage}" style="color:red; text-size:20" />
<h:form>
<h:panelGrid>
<h:outputText value="Username" />
<h:inputText value="#{userBean.username}" id="username" required="true" />
<h:message for="username" style="color:red; text-size:18"/>
<h:outputText value="Password" />
<h:inputSecret value="#{userBean.password}" id="password" required="true"/>
<h:message for="password" style="color:red; text-size:18"/>
<h:commandButton value="Login" action="#{userBean.verifyUser}" />
</h:panelGrid>
</h:form>
</body>
</f:view>
</html>
When I am deploying this to Tomcat7 then complete page is displayed. However, when I am deploying the same on JBoss6.1 Runtime, The form elements corresponding to JSF's HTML tag library are not displayed. I'm only seeing content from plain HTML elements like <h1> and <h2>.
Where am I committing the mistake?
Previously, I was having problem with my JSF2.0 page not being rendered completely.
However, I came to figure out my stupidity in my config-faces.xml where i have used:
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
version="1.2">
...
</faces-config>
instead of :
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
...
</faces-config>
The kind of problem which arises when using Copy-Paste mechanism without care.

XSLT current-groups() issue

I've been having a bit of trouble with an HTML file that I'm trying to translate. Basically, the relevant part of the source structure as it currently stands is this:
<h2 />
<h3 />
<table />
<table />
<h3 />
<table />
<table />
<h3 />
<table />
<h3 />
<h3 />
<table />
<table />
<h2 />
<h3 />
...
and so on. Each of the contents of these are being translated in different ways, but the problem I'm currently having is in grouping them correctly. Essentially, I want it to end up like the following:
<category>
<h2 />
<container>
<h3 />
<table />
<table />
</container>
<container>
<h3 />
<table />
<table />
</container>
<container>
<h3 />
<table />
</container>
<container>
<h3 />
</container>
<container>
<h3 />
<table />
<table />
</container>
</category>
<category>
<h2 />
<container>
<h3 />
...
to achieve this, I've been using the following code:
<xsl:for-each-group select="node()"group-starting-with="xh:h2">
<category>
<xsl:apply-templates select="xh:h2"/>
<xsl:for-each-group select="current-group()"
group-starting-with="xh:h3">
<container>
<xsl:apply-templates select="current-group()[node()]"/>
</container>
</xsl:for-each-group>
</category>
</xsl:for-each-group>
However, the output I get from this is as follows:
<category>
<h2 />
<container>
<h3 />
<table />
<table />
<h3 />
<table />
<table />
<h3 />
<table />
<h3 />
<h3 />
<table />
<table />
</container>
</category>
<category>
<h2 />
<container>
<h3 />
...
The first for-loop function is working as expected, however the second does not appear to be. If I use <xsl:copy-of> to output the first element in the <current-group> in the second for-loop, it shows the <h2> element, where that element should not even be in the group.
If anyone can point out where I'm going wrong, or offer a better solution, it would be greatly appreciated.
I think you want to change
<xsl:for-each-group select="node()" group-starting-with="xh:h2">
<category>
<xsl:apply-templates select="xh:h2"/>
<xsl:for-each-group select="current-group()"
group-starting-with="xh:h3">
to
<xsl:for-each-group select="*" group-starting-with="xh:h2">
<category>
<xsl:apply-templates select="."/>
<xsl:for-each-group select="current-group() except ."
group-starting-with="xh:h3">
That way the inner for-each-group processes the h3 and table elements but no the h2 element starting the outer group.
If you need more help then consider to post small but complete samples with namespaces present allowing us to reproduce the problem with the undesired output.
I think you've simplified the problem and in doing so have introduced some red herrings.
The xsl:apply-templates select="h2" surely does nothing, because none of the nodes selected in the outer grouping has an h2 child.
In every group selected by the outer for-each-group, except the first, the first node in the group will be an h2 element, by definition. Your inner for-each-group will partition the sequence of nodes starting with an h2 into: first, a group that starts with the h2 (because every node becomes part of some group), and then a sequence of groups each of which starts with an h3. You need to split out the first (non-h3) group and treat it differently, because you don't want to generate a container element in this case. So you need an xsl:choose in the inner for-each-group, typically with the condition xsl:when test="self::h2" to detect that you're processing the special first group.
Having said all that I can't see why you aren't getting a container element for each h3 element. I think this must be caused by something that you haven't shown us (perhaps a namespace issue?)

Spring Security custom form always fails

I am trying to give my website (created using JSF 2) a custom login from using Spring Security. When I use the default login form, the website works fine. However, when I try to use my own custom login form, I am always directed to my authentication-failure-url, even when I type the correct username/password. Does anyone know why this is or how I can fix it? My code is below.
security-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<sec:http auto-config="true" use-expressions="true">
<sec:intercept-url pattern="/login.jsf" access="isAnonymous()" />
<sec:intercept-url pattern="/pages/secure/**" access="hasRole('ROLE_USER')" />
<sec:intercept-url pattern="/pages/home/**" access="hasRole('ROLE_USER')" />
<sec:intercept-url pattern="/pages/unsecure/**" access="permitAll"/>
<sec:form-login login-page="/login.jsf" default-target-url="/pages/home/home.jsf"
authentication-failure-url="/fail.jsf"/>
</sec:http>
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider>
<sec:user-service>
<sec:user authorities="ROLE_USER" name="admin" password="admin" />
</sec:user-service>
</sec:authentication-provider>
</sec:authentication-manager>
</beans:beans>
login.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<title>Login</title>
<h:outputStylesheet name="css/styles.css" />
<h:outputScript name="jquery/jquery-plugins.js" library="primefaces" />
</h:head>
<h:body>
<div class="login">
<form method="POST" action="j_spring_security_check">
<h:inputText name="j_username" id="username"></h:inputText>
<p:watermark for="username" value="Username" />
<br />
<h:inputSecret name="j_password" id="password">
</h:inputSecret>
<p:watermark for="password" value="Password" />
<br />
<h:commandButton name="submit" type="submit" value="Submit"></h:commandButton>
</form>
</div>
</h:body>
</html>
Have you looked at the generated HTML output? Rightclick page in browser, do View Source to see it.
The name attribute of
<h:inputText name="j_username" id="username"></h:inputText>
<h:inputSecret name="j_password" id="password"></h:inputSecret>
isn't been generated at all! Instead, the client ID represents the element's name.
Fix it accordingly:
<h:inputText id="j_username" />
<p:watermark for="j_username" value="Username" />
<h:inputSecret id="j_password" />
<p:watermark for="j_password" value="Password" />
You could of course also just use plain vanilla HTML:
<input type="text" name="j_username" placeholder="Username" />
<input type="password" name="j_password" placeholder="Password" />
Try setting the following as action of the form.
action="#{request.contextPath}/j_spring_security_check"

Grouping in XSLT 2.0 similar to br to p problems

In XSLT 1.0, a common question in forums was how to convert flat HTML into hierarchical XML, which many times boiled down to nesting text in between <br /> tags in <p> tags.
I have a similar problem, which I think I've partially solved using XSLT 2.0, but it's a new approach to me and I'd like to get a second opinion.
The XHTML source has <span class="pageStart"></span> scattered throughout. They can appear in several different parent nodes. I want to wrap all the nodes between one page start marker and the next in an <page> node. The solution I currently have is:
<xsl:template match="*[child::span[#class='pageStart']]">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:for-each-group select="node()"
group-starting-with="span[#class='pageStart']">
<page>
<xsl:apply-templates select="current-group()"/>
</page>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
There's at least one flaw with this -- the parent node of the marker gets a <page> as a child node when I don't want it. In other works, if there's a <div> that has a child page marker anywhere in it, an <page> node is created as an immediate child of <div> in addition to the locations I expect.
I had hoped that I could simply make the template rule be <xsl:template match="span[#class='pageStart']"> but current-group() seems to be empty no matter what I try. The common sense approach I tried was <xsl:for-each-group select="node()" group-starting-with="span[#class='pageStart']">.
Is there an easier way to solve this problem that I'm missing?
EDIT
Here's an example of the input:
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head></head>
<body>
<span class="pageStart"/>
<p>...</p>
<div>...</div>
<img />
<p></p>
<span class="pageStart"/>
<div>...</div>
<span class="pageStart"/>
<p>...</p>
<div>
<span class="pageStart"/>
<p>...</p>
<p>...</p>
<span class="pageStart"/>
<div>...</div>
<img/>
</div>
</body>
</html>
I assume the last two nested pages make this problem more difficult, so I'd be perfectly happy getting this as the output, or something close:
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head></head>
<body>
<page>
<span class="pageStart"/>
<p>...</p>
<div>...</div>
<img />
<p></p>
</page>
<page>
<span class="pageStart"/>
<div>...</div>
</page>
<page>
<span class="pageStart"/>
<p>...</p>
<div>
<page>
<span class="pageStart"/>
<p>...</p>
<p>...</p>
</page>
<page>
<span class="pageStart"/>
<div>...</div>
<img/>
</page>
</div>
</page>
</body>
</html>
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[span/#class='pageStart']">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:for-each-group select="node()"
group-starting-with="span[#class='pageStart']">
<page>
<xsl:apply-templates select="current-group()"/>
</page>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<html>
<head></head>
<body>
<span class="pageStart"/>
<p>...</p>
<div>...</div>
<img />
<p></p>
<span class="pageStart"/>
<div>...</div>
<span class="pageStart"/>
<p>...</p>
<div>
<span class="pageStart"/>
<p>...</p>
<p>...</p>
<span class="pageStart"/>
<div>...</div>
<img/>
</div>
</body>
</html>
produces the wanted, correct result:
<html>
<head/>
<body>
<page>
<span class="pageStart"/>
<p>...</p>
<div>...</div>
<img/>
<p/>
</page>
<page>
<span class="pageStart"/>
<div>...</div>
</page>
<page>
<span class="pageStart"/>
<p>...</p>
<div>
<page>
<span class="pageStart"/>
<p>...</p>
<p>...</p>
</page>
<page>
<span class="pageStart"/>
<div>...</div>
<img/>
</page>
</div>
</page>
</body>
</html>

Resources