How to get sum of elements by it's position. I have below XML and wanted to get sum of DonneeRensCompl by position using XSLT - xslt-1.0

I have below code and wanted to get sum of DonneeRensCompl by it's position. Example Position1 = 3.60 (1.30+ 2.30 = 3.60) likewise for all possible positions.
<DATA_DS>
<R>
<Annee>2021</Annee>
<CaseRensCompl>
<CodeRensCompl>235</CodeRensCompl>
<DonneeRensCompl>1.30</DonneeRensCompl>
</CaseRensCompl>
<CaseRensCompl>
<CodeRensCompl>B-1</CodeRensCompl>
<DonneeRensCompl>10650.00</DonneeRensCompl>
</CaseRensCompl>
<CaseRensCompl>
<CodeRensCompl>RZ-RJ</CodeRensCompl>
<DonneeRensCompl>10650.00</DonneeRensCompl>
</CaseRensCompl>
</R>
<R>
<Annee>2021</Annee>
<CaseRensCompl>
<CodeRensCompl>235</CodeRensCompl>
<DonneeRensCompl>2.30</DonneeRensCompl>
</CaseRensCompl>
<CaseRensCompl>
<CodeRensCompl>RZ-CA</CodeRensCompl>
<DonneeRensCompl>10650.00</DonneeRensCompl>
</CaseRensCompl>
</R>
</DATA_DS>

If you don't know in advance the number of positions, then you must increment the position in a loop until there are no nodes in the current position:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/DATA_DS">
<root>
<xsl:call-template name="sum-by-positions"/>
</root>
</xsl:template>
<xsl:template name="sum-by-positions">
<xsl:param name="i" select="1"/>
<xsl:variable name="cases" select="//CaseRensCompl[$i]" />
<xsl:if test="$cases">
<sum position="{$i}">
<xsl:value-of select="format-number(sum($cases/DonneeRensCompl), '#,##0.00')"/>
</sum>
<!-- recursive call -->
<xsl:call-template name="sum-by-positions">
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, this will produce:
Result
<?xml version="1.0" encoding="UTF-8"?>
<root>
<sum position="1">3.60</sum>
<sum position="2">21,300.00</sum>
<sum position="3">10,650.00</sum>
</root>
To understand how this works, read this note in the XPath 1.0 specification:
NOTE: The location path //para[1] does not mean the same as the location path /descendant::para[1]. The latter selects the first descendant para element; the former selects all descendant para elements that are the first para children of their parents.

Related

XSLT 1.0: How to determine the position of a node based on the value

thanks for taking the time!
I am trying to get the position of a node based on its value, so that I can then get corresponding entries. Please let me show you with an example, it will be easier to explain.
Here is the source XML:
<?xml version="1.0" encoding="UTF-8"?>
<sampleSourceXML>
<Table>
<Header>
<Column>Name</Column>
<Column>Surname</Column>
<Column>Title</Column>
</Header>
<Body>
<Row>
<Entry>James</Entry>
<Entry>Bond</Entry>
<Entry>Mr</Entry>
</Row>
<Row>
<Entry>Harry</Entry>
<Entry>Potter</Entry>
<Entry>Mr</Entry>
</Row>
</Body>
</Table>
</sampleSourceXML>
This is the resulting XML that I would like to achieve:
<?xml version="1.0" encoding="UTF-8"?>
<sampleResultXML>
<Person>
<FirstName>James</FirstName>
<LastName>Bond</LastName>
<Title>Mr</Title>
</Person>
<Person>
<FirstName>Harry</FirstName>
<LastName>Potter</LastName>
<Title>Mr</Title>
</Person>
</sampleResultXML>
I tried it with this XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:element name="sampleResultXML">
<xsl:for-each select="sampleSourceXML/Table/Rows/Row">
<xsl:element name="Person">
<xsl:element name="FirstName">
<xsl:value-of select="./Entry[1]/text()"></xsl:value-of>
</xsl:element>
<xsl:element name="LastName">
<xsl:value-of select="./Entry[2]/text()"></xsl:value-of>
</xsl:element>
<xsl:element name="Title">
<xsl:value-of select="./Entry[3]/text()"></xsl:value-of>
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This works.
But! The source XML can have a different order. For example the Title element can be above the Name element. So the hardcoded position in my XSLT (e.g. Entry[3]) will not work any more.
The header order and the entry order are always corresponding. So if the Column Title is in position 3, so will be the corresponding row.
So I need a way to get the position of, let's say, the Title element dynamically. So I have to go through the Column elements until I reach the one with value "Title". This position I thought I'd save in a variable (e.g. Position_Title) so that I can fill the value later like so:
<xsl:element name="Title">
<xsl:value-of select="./Entry[$Position_Title]/text()"></xsl:value-of>
</xsl:element>
However I haven't been able to find a way to do this...
Do you have any ideas how I can achieve this?
Thanks!
Nick
EDIT: Thanks to Michael for the help! Here is the final XSLT I'm using:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/sampleSourceXML">
<xsl:variable name="cols" select="Table/Header/Column"/>
<sampleResultXML>
<xsl:for-each select="Table/Body/Row">
<Person>
<xsl:for-each select="Entry">
<xsl:variable name="i" select="position()"/>
<xsl:variable name="elementName">
<xsl:choose>
<xsl:when test="$cols[$i]='Name'">
<xsl:value-of select="'FirstName'"/>
</xsl:when>
<xsl:when test="$cols[$i]='Surname'">
<xsl:value-of select="'LastName'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$cols[$i]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$elementName}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Person>
</xsl:for-each>
</sampleResultXML>
</xsl:template>
</xsl:stylesheet>
So I need a way to get the position of, let's say, the Title element dynamically. So I have to go through the Column elements until I reach the one with value "Title". This position I thought I'd save in a variable (e.g. Position_Title) so that I can fill the value later like so:
I think it could be much simpler if you do it from the opposite direction. Try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/sampleSourceXML">
<xsl:variable name="cols" select="Table/Header/Column" />
<sampleResultXML>
<xsl:for-each select="Table/Body/Row">
<Person>
<xsl:for-each select="Entry">
<xsl:variable name="i" select="position()" />
<xsl:element name="{$cols[$i]}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</Person>
</xsl:for-each>
</sampleResultXML>
</xsl:template>
</xsl:stylesheet>

XPTY0004: An empty sequence is not allowed as the value of variable while loading xslt using saxon templatefactory

I updated the provided xslt to accept a param "multiplexpaths" from my source and assignin enter code here`g this to nodes variable in xslt to below:
<?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"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="multiplexpaths" as="xs:string" static="yes" />
<!-- xsl:param name="copy" as="xs:string" static="yes" select="'//other[. = 1345], //more[. = 2]'"/-->
<xsl:variable name="nodes" _select="{$multiplexpaths}"/>
<xsl:variable name="ancestors" select="$nodes/ancestor::*"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="$nodes">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="$ancestors">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
For the xsl:evaluate example, you would need to use Saxon 10 HE, 9.9 HE doesn't support xsl:evaluate.
As for setting a static parameter with Saxon 9.9 and s9api, a simple example is
String paths = "catalog/cd1,catalog/Test/value/a1";
Processor processor = new Processor(true);
XsltCompiler xsltCompiler = processor.newXsltCompiler();
xsltCompiler.setParameter(new QName("copy"), new XdmAtomicValue(paths));
XsltExecutable xsltExecutable = xsltCompiler.compile(new StreamSource("static-param-example2.xsl"));
Xslt30Transformer xslt30Transformer = xsltExecutable.load30();
xslt30Transformer.transform(new StreamSource("input-sample1.xml"), xslt30Transformer.newSerializer(System.out));
with the input-sample1.xml being
<catalog>
<cd1>
<name>abc</name>
<Stext>1234</Stext>
<Tag>uuuu</Tag>
</cd1>
<cd2>
<name>abc</name>
<Stext>1234</Stext>
<Tag>uuuu</Tag>
</cd2>
<Test>
<value>
<a1>123</a1>
<b1>77474</b1>
</value>
</Test>
</catalog>
and the XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="copy" as="xs:string" static="yes" select="'()'"/>
<xsl:variable name="nodes" _select="{$copy}"/>
<xsl:variable name="ancestors" select="$nodes/ancestor::*"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="$nodes">
<xsl:sequence select="."/>
</xsl:template>
<xsl:template match="$ancestors">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
this outputs
<catalog>
<cd1>
<name>abc</name>
<Stext>1234</Stext>
<Tag>uuuu</Tag>
</cd1>
<Test>
<value>
<a1>123</a1>
</value>
</Test>
</catalog>
So the main point is to set the parameter on the XsltCompiler and not after compilation on the transformer.

XSLT sort data in variable and save them to another variable

I have a problem. I have the following XML
<countryList>
<country>
<name>Afghanistan</name>
<population>29117000</population>
<area>654329</area>
</country>
<country>
<name>Albania</name>
<population>3195000</population>
<area>28748</area>
</country>
<country>
<name>Algeria</name>
<population>35423000</population>
<area>2381741</area>
</country>
<country>
<name>Andorra</name>
<population>84082</population>
<area>468</area>
</country>
</countryList>
I have one question. All I need to do is divide population/area and sort these divisions for each country. However, I tried this
<xsl:variable name="Podiel">
<xsl:value-of select="../population div ../area"/>
</xsl:variable>
<xsl:variable name="PodielPodiel">
<xsl:for-each select="$Podiel">
<xsl:sort select="." data-type="number" order="descending"/>
</xsl:for-each>
</xsl:variable>
but I still get error
The 'select' expression does not evaluate to a node set.
no result for data1.xml
Any help? I just want to know the maximum of all divisions.
Thanks.
Not sure whether the issue has been resolved, however for using node-set in XSLT 1.0, extension functions have to be used. Please refer to node-set extension for additional details on it's usage in XSLT 1.0.
If at all the country list in the input XML needs to be sorted on the value of population div area, the number() function can be used within <xsl:sort>. Declaring variables and calculating the additional values and then accessing the variable is not required.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" />
<xsl:strip-space elements="*" />
<!-- identity transform -->
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="countryList">
<xsl:copy>
<xsl:apply-templates select="country">
<xsl:sort select="number(population div area)" data-type="number" order="descending" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output
<countryList>
<country>
<name>Andorra</name>
<population>84082</population>
<area>468</area>
</country>
<country>
<name>Albania</name>
<population>3195000</population>
<area>28748</area>
</country>
<country>
<name>Afghanistan</name>
<population>29117000</population>
<area>654329</area>
</country>
<country>
<name>Algeria</name>
<population>35423000</population>
<area>2381741</area>
</country>
</countryList>

xslt 3.0 xsl:evaluate example

For the following xml document:
<company>
<employee>
<name>John Andrews</name>
<age>23</age>
<salary>4000</salary>
<division>Accounting</division>
</employee>
</company>
I have the xsl like
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="company/employee/name"/>
<xsl:variable name="test">
<xsl:text>company/employee/name</xsl:text>
</xsl:variable>
<xsl:evaluate xpath="$test"/>
</xsl:template>
</xsl:stylesheet>
How can I use $test variable in xsl:evaluate in order to get the same result as in the:
<xsl:value-of select="company/employee/name"/>
Is it possible?
You need to set the context item with e.g.
<xsl:evaluate xpath="$test" context-item="."/>

Setting xpath as value to xslt variable and pass it as a paramter to a JS Function

Could you explain how to set xpath of a node as the value to a xslt variable . I also want to display the value and then pass it as a parameter to a JS Function. Thanks for all your Help.
XPath expressions are not values, so they cannot be bound to variables. You can of course put an XPath expression in a string. There's no standard way in XSLT to execute an XPath expression held in a string, but many implementations have an extension function (such as xx:evaluate()) to do it.
It helps to explain what problem you are trying to solve, not what technique you are trying to use to solve it. Then we could suggest alternative and perhaps better approaches.
Sample Input XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>
<trial>test</trial>
</child>
</root>
Model code to pass XPath of current node to inline JS function:
<?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" xmlns:js="urn:js">
<xsl:output method="xml" indent="yes"/>
<msxsl:script language="JScript" implements-prefix="js">
<![CDATA[
function test(something)
{
//code here
}
]]>
</msxsl:script>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="trial">
<xsl:copy>
<xsl:variable name="xpath">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="name()"/>
<xsl:if test="not(position()=last())">
<xsl:value-of select="'/'"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:apply-templates select="js:test($xpath)"/> <!--Passed value $xpath to funciton test-->
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
having return value as
return (something);
will output:
<?xml version="1.0" encoding="utf-8"?>
<root>
<child>
<trial>root/child/trial</trial>
</child>
</root>