XSL:IF within XSL:for-each-group - xslt

In my XSLT, I have something like:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="PhyscianTotals" name="PhyscianTotals">
<xsl:for-each select="PhysicianTotals">
<xsl:for-each-group select="Statistic" group-by="Type">
<xsl:if test="Title='PHYSICIAN DETAIL TOTAL'">
<xsl:element name="totals">
</xsl:element>
</xsl:if>
</xsl:for-each-group>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Is this valid XSLT? Specifically, the section of "xsl:if within the xsl:for-each-group". One of the XSLT compilation tool we call always error out stating: xsl:if is not allowed at this position in the stylesheet. If I remove the xsl:for-each-group, it passes. I am not sure if it's my xslt having errors or if it's the compilation tool.
Turns out our tool only support XSLT 1.0. So I guess I am back to rewrite the XSLT using 1.0 tags only.
The original XML looks like:
<?xml version="1.0" encoding="UTF-8"?>
<PhysicianTotals>
<Statistic>
<Title>PHYSICIAN TOTAL</Title>
<Type>Type 1</Type>
<Key>Cases</Key>
<Value>1</Value>
</Statistic>
<Statistic>
<Title>PHYSICIAN TOTAL</Title>
<Type>Type 1</Type>
<Key>Percentage</Key>
<Value>25.0%</Value>
</Statistic>
<Statistic>
<Title>PHYSICIAN TOTAL</Title>
<Type>Type 2</Type>
<Key>Cases</Key>
<Value>3</Value>
</Statistic>
<Statistic>
<Title>PHYSICIAN TOTAL</Title>
<Type>Type 1</Type>
<Key>Percentage</Key>
<Value>75.0%</Value>
</Statistic>
</PhysicianTotals>
And the output will look like:
<?xml version="1.0" encoding="UTF-8"?>
<totals>
<type>PHY_DETAIL</type>
<detailInfo>
<code>Type 1</code>
</detailInfo>
<count>
<caseValue>1</caseValue>
<percentValue>25.0%</percentValue>
</count>
</totals>
<totals>
<type>PHY_DETAIL</type>
<detailInfo>
<code>Type 2</code>
</detailInfo>
<count>
<caseValue>3</caseValue>
<percentValue>75.0%</percentValue>
</count>
</totals>

Apart from the copy/paste error in the xsl:output declaration, your code looks perfectly OK to me. It's a bit suspect - do you really have an element called PhyscianTotals with a child called PhysicianTotals - so I suspect you're not showing us the code that actually generates the error.
Another possibility is that the tool generating the error is an XSLT 1.0 processor.

enchttp://stackoverflow.com/editing-helpoding="UTF-8"
This is syntactically/lexically illegal name: enchttp://stackoverflow.com/editing-helpoding
Did you mean encoding?
Apart from this there are no other lexical/syntax errors, and it seems that the transformation has a number of logical issues and is likely not to produce the wanted result -- but I cannot be 100% certain without seeing the source XML document and the desired result.

Related

XSLT 1.0: How to combine and sum the fields of children in records based on having the same id field?

I have the following:
<ns0:tXML>
<Message>
<Report>
<Page>
<PageID>01</PageID>
<PageDetail>
<PageName>11</PageName>
<Totals>
<Num>10</Num>
</Totals>
</PageDetail>
<PageDetail>
<PageName>11</PageName>
<Totals>
<Num>5</Num>
</Totals>
</PageDetail>
</Page>
<Page>
<PageID>02</PageID>
<PageDetail>
<PageName>12</PageName>
<Totals>
<Num>10</Num>
</Totals>
</PageDetail>
<PageDetail>
<PageName>12</PageName>
<Totals>
<Num>3</Num>
</Totals>
</PageDetail>
</Page>
</Report>
</Message>
</ns0:tXML>
I want to make the output so that PageDetails are combined for each Page as long as their PageName and PageID are the same, including summing the values of the combined.
Output Wanted:
<ns0:tXML>
<Message>
<Report>
<Page>
<PageID>01</PageID>
<PageDetail>
<PageName>11</PageName>
<Totals>
<Num>15</Num>
</Totals>
</PageDetail>
</Page>
<Page>
<PageID>02</PageID>
<PageDetail>
<PageName>12</PageName>
<Totals>
<Num>13</Num>
</Totals>
</PageDetail>
</Page>
</Report>
</Message>
</ns0:tXML>
How would I go about it? All efforts with using keys and playing with templates has led to cases where only one of the Pages got created, or it combined all the Pages no matter where they were on the xml, showing that I was likely trying to do an all apply to it rather than sticking to the current context.
Let's start from a little correction to your source. It should include
the namespace specification:
<ns0:tXML xmlns:ns0="urn.dummy.com">
otherwise there is reported the following error:
The prefix "ns0" for element "ns0:tXML" is not bound.
One of possible solutions is to use the following script:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="urn.dummy.com">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="Pd" match="PageDetail" use="concat(../PageID, '|', PageName)"/>
<xsl:template match="Page">
<xsl:copy>
<xsl:copy-of select="PageID"/>
<xsl:for-each select="PageDetail[generate-id()=generate-id(key('Pd',
concat(../PageID,'|', PageName))[1])]">
<xsl:variable name="kk" select="concat(../PageID,'|', PageName)"/>
<xsl:copy>
<xsl:copy-of select="PageName"/>
<xsl:element name="Totals">
<xsl:element name="Num">
<xsl:value-of select="sum(key('Pd', $kk)/Totals/Num)"/>
</xsl:element>
</xsl:element>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>
For a working example, generating just your expected result,
see: http://xsltransform.net/93YRmgt

XSLT: Test the contents of a result-document in XSpec?

I'm writing a program that uses XSLT and need to test the contents of a result-document call in Xspec. In the example below, I would like to test the contents of result.xml. If this is possible, how do you do this?
XML: test.xml
<?xml version="1.0" encoding="UTF-8"?>
<root></root>
XSLT: result-document.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:template match="/root">
<xsl:result-document href="result.xml">
<my-result></my-result>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
XSpec:
<x:description xmlns:x="http://www.jenitennison.com/xslt/xspec" stylesheet="result-document.xsl">
<x:scenario label="Test result document">
<x:context href="test.xml"></x:context>
<!-- How do you test the result.xml file here? -->
<x:expect label="test result">
<my-result></my-result>
</x:expect>
</x:scenario>
</x:description>
I was looking for the same answer, but I suspect this is an inherent limitation in how XSpec is implemented.
I suspect the work-around is to have one untestable template that uses the result-document, then use a match or call template within the result-document that can be tested:
XSLT
<xsl:template match="/root">
<xsl:result-document href="result.xml">
<xsl:call-template name="my-result"/>
</xsl:result-document>
</xsl:template>
<xsl:template name="my-result">
<!-- content -->
</xsl:template>
XSPEC
<x:scenario label="when calling my-result">
<x:call template="my-result"/>
<x:expect label="test something" pending="add your tests here"/>
</x:scenario>
So you can't really test that the result documents are being made in the right way, but you can check their results.
Note that XSPEC won't literally "test the contents of result.xml"; just the behaviour of the XSLT on the inputs that you specify in the XSPEC itself.

Two phase XSLT transformation converting string to XML first

I have the following XML:
<?xml version="1.0" encoding="utf-8"?>
<string>
<Table>
<Rows>
<Row Id="0">
<Column Name="INS_NAME" XPath="Ins.Name">Jane</Column>
<Column Name="INS_LASTNAME" XPath="Ins.LastName">Smith</Column>
</Row>
<Row Id="1">
<Column Name="INS_NAME" XPath="Ins.Name">Joe</Column>
<Column Name="INS_LASTNAME" XPath="Ins.LastName">Miller</Column>
</Row>
<Row Id="2">
<Column Name="INS_NAME" XPath="Ins.Name">George</Column>
<Column Name="INS_LASTNAME" XPath="Ins.LastName">Ramsey</Column>
</Row>
</Rows>
</Table>
</string>
and I would like to transform it to this XML using a single XSLT:
<?xml version="1.0" encoding="utf-8"?>
<Customers>
<Customer><Name>Jane</Name><LastName>Smith</LastName></Customer>
<Customer><Name>Joe</Name><LastName>Miller</LastName></Customer>
<Customer><Name>George</Name><LastName>Ramsey</LastName></Customer>
</Customers>
I can do it with two different XSLT's:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="/" disable-output-escaping="yes" />
</xsl:template>
</xsl:stylesheet>
and then:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Customers>
<xsl:for-each select="Table/Rows/Row">
<Customer>
<Name><xsl:value-of select="Column[#Name='INS_NAME']" /></Name>
<LastName><xsl:value-of select="Column[#Name='INS_LASTNAME']" /></LastName>
</Customer>
</xsl:for-each>
</Customers>
</xsl:template>
</xsl:stylesheet>
I have been reading about multi phase transformations but I can't seem to get it. I have tried saving the first XSLT in a variable but it seems disable-output-escaping="yes" does not work when saving to a variable.
Can anybody help?
Thank you.
New information (Edit)
I am now translating the string this way:
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="stringXml">
<?xml version="1.0" encoding="utf-8"?>
<xsl:value-of select="translate(translate(/,'>','>'),'<','<')" />
</xsl:variable>
...
How can I do a transformation on the resulting XML stored in stringXML?
Final Solution (Edit)
<msxml:script implements-prefix="myLib" language="C#">
<msxml:assembly name="System.Web"/>
<msxml:using namespace="System.Web"/>
<![CDATA[
public System.Xml.XPath.XPathNodeIterator convertText(string text)
{
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.LoadXml(text);
return doc.CreateNavigator().Select("/");
}
]]>
</msxml:script>
it seems disable-output-escaping="yes" does not work when saving to a
variable.
Your observation is correct.
DOE only affects the serialization of the (final) result of the transformation and isn't applied on intermediary trees.
Here is what the W3C XSLT 1.0 specification explicitly says:
"An XSLT processor will only be able to disable output escaping if it
controls how the result tree is output. This may not always be the
case. For example, the result tree may be used as the source tree for
another XSLT transformation instead of being output."
The same negative answer holds for trying to use a variable, whose value is a string, containing a textual representation of an XML document.
I had a similar situation where I needed to parse an escaped XML inside my actual XML. I will post up my solution to also help someone else. Please also note that I am also using Saxon-PE parser.
In my situation I have the original XML that contains an escaped XML in a child node. I needed to get the inner XML inside the RootNode of the escaped XML.
Source XML:
<?xml version="1.0" encoding="utf-8"?>
<MyTestXml>
<SomeXmlStuff>
<Text1>Hello</Text1>
<Text2>World</Text2>
</SomeXmlStuff>
<SomeEscapedXml><RootNode><FirstNode>Hello</FirstNode><SecondNode>World</SecondNode><ThirdNode>Again</ThirdNode></RootNode></SomeEscapedXml>
</MyTestXml>
When you unescaped the XML, it looks like this:
<RootNode>
<FirstNode>Hello</FirstNode>
<SecondNode>World</SecondNode>
<ThirdNode>Again</ThirdNode>
</RootNode>
With the following XSLT transformation is applied on the source XML:
<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/"
exclude-result-prefixes="xsl saxon">
<xsl:template match="/">
<MyOutput>
<xsl:call-template name="GetRootNodeInnerXml">
<xsl:with-param name="escapedXml" select="MyTestXml/SomeEscapedXml" />
</xsl:call-template>
</MyOutput>
</xsl:template>
<xsl:template name="GetRootNodeInnerXml">
<xsl:param name="escapedXml" required="yes" />
<xsl:copy-of select="saxon:parse($escapedXml)/RootNode/node()"/>
<!-- You can also use this line below if you're not using saxon parser. Just make sure your parser supports XSL 3.0 -->
<!--
<xsl:copy-of select="fn:parse-xml($escapedXml)/RootNode/node()" xmlns:fn="http://www.w3.org/2005/xpath-functions"/>
-->
</xsl:template>
</xsl:stylesheet>
This gives you the following output:
<?xml version='1.0' ?>
<MyOutput>
<FirstNode>Hello</FirstNode>
<SecondNode>World</SecondNode>
<ThirdNode>Again</ThirdNode>
</MyOutput>

exclude empty elements

in my sample xml file, i have this:
<AAA mandatory = "true"> good </AAA>
<BBB mandatory = "true"></BBB>
<CCC />
in the resulting xml, the result should be like this:
<AAA> good </AAA>
<BBB></BBB>
what should i put in my transformation file xslt to produce this xml?
currently, i have this:
<xsl:template match="node()[(#mandatory='true' or (following-sibling::*[#mandatory='true' and string-length(normalize-space(.)) > 0] or preceding-sibling::*[#mandatory='true' and string-length(normalize-space(.)) > 0])) or descendant-or-self::*[string-length(normalize-space(.)) > 0]]">
but this keeps displaying
<CCC />
When I run your XSLT on the input XML I do not get any output.
Your provided XML is not well formed and the XPATH in your "match" is too complicated I think.
I came up with a XSL 1.0 solution but I do not know if you can use that in XSL 2.0. I do not have experience with XSL 2.0.
This XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<list>
<xsl:apply-templates/>
</list>
</xsl:template>
<xsl:template match="*[#mandatory='true']">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
applied to this input XML:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<AAA mandatory="true"> good </AAA>
<BBB mandatory="true"/>
<CCC/>
</list>
gives this output XML:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<AAA> good </AAA>
<BBB/>
</list>
I am not sure if you also want to check on the text length of an element or only on the attribute mandatory. I only check on the attribute in my XSL.

Namespace prefix is missing after XSLT transformation of XML using Apache Xalan

I am having the following XML:
<?xml version="1.0"?>
<abc:Element1 xmlns:abc="http://..../resources/abc/v2/"
...>
<abc:Element2>
<abc:Element3s>
<abc:Element4 name="name1"
resourceRef="name2"/>
</abc:Element3s>
</abc:Element2>
<abc:Resources>
<abc:Resource xsi:type="abc:Something"
name="name2"/>
</abc:Resources>
</abc:Element1>
... and this XSLT stylesheet:
<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:abc="http://.../resources/abc/v2/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
<checker name="something">
<xsl:for-each select="abc:Element1/abc:Element2/abc:Element3s/abc:Element4">
<xsl:variable name="resource" select="#resourceRef"/>
<xsl:variable name="xsiType"><xsl:value-of select="//abc:Resource[#name=$resource]/#xsi:type"/></xsl:variable>
<xsl:choose>
<xsl:when test="$xsiType='abc:Something'">
...
</xsl:when>
<xsl:otherwise>
...
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</checker>
</xsl:template>
</xsl:stylesheet>
I am using XALAN 2.7.1 with org.apache.xalan.xsltc.trax.TransformerFactoryImpl (also tried with org.apache.xalan.processor.TransformerFactoryImpl -> same result) to transform the XML.
I expect the following line to store abc:Something in variable xsiType.
<xsl:variable name="xsiType"><xsl:value-of select="//abc:Resource[#name=$resource]/#xsi:type"/></xsl:variable>
but unfortunatley only Something (without namespace as prefix) is stored in xsiType. I verified this because
<xsl:when test="$xsiType='abc:Something'">
is not true.
I also transformed the XMl using xsltproc and the resulting XML looks as expected. Therefore, I expect the input XML/XSLT stylesheet to be correct. I assume something is wrong with Xalan and its configuration.
Can anyone help?
Your sample data is not well-formed, so it's difficult to tell. This is likely to be a namespace issues. Here's a sanitized version of your input and stylesheet that extracts the data you want:
<?xml version="1.0"?>
<abc:Element1
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:abc="http://resources/abc/v2">
<abc:Element2>
<abc:Element3s>
<abc:Element4 name="name1" resourceRef="name2"/>
</abc:Element3s>
</abc:Element2>
<abc:Resources>
<abc:Resource xsi:type="abc:Something" name="name2"/>
</abc:Resources>
</abc:Element1>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:abc="http://resources/abc/v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="abc xsi"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="resource" select="'name2'"/>
<xsl:variable name="type"
select="//abc:Resource[#name=$resource]/#xsi:type"/>
<checker name="{ $type }"/>
</xsl:template>
</xsl:stylesheet>
This will produce:
<?xml version="1.0"?>
<checker name="abc:Something"/>
It looks to me like a problem specific to Xalan. It could however be a problem with the underlying XML parser: the default parser in th Sun JDK has some weird bugs including some that corrupt attribute values. Always use the Apache versions of Xalan and Xerces rather than the versions that come with the JDK. And of course, if you're using Xalan then it's almost zero cost to switch to Saxon, which gives you all the benefits of XSLT 2.0.