I am new to xslt. I have a xml file like
<EmpCollection>
<Emp>
<Name>Sai</Name>
<Sal>7000</Sal>
</Emp>
<Emp>
<Name>Nari</Name>
<Sal>7400</Sal>
</Emp>
<Emp>
<Name>Hari</Name>
<Sal>9000</Sal>
</Emp>
<Emp>
<Name>Suri</Name>
<Sal>8900</Sal>
</Emp>
</EmpCollection>
Now I want to add sum of salaries without using sum() function.
I want to learn xslt in a clear way:-)
I. Probably one of the simplest solutions:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Sal">
<xsl:param name="pSum" select="0"/>
<xsl:apply-templates select="following::Sal[1]">
<xsl:with-param name="pSum" select="$pSum + ."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Sal[not(following::Sal)]">
<xsl:param name="pSum" select="0"/>
<xsl:value-of select="$pSum + ."/>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="descendant::Sal[1]"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<EmpCollection>
<Emp>
<Name>Sai</Name>
<Sal>7000</Sal>
</Emp>
<Emp>
<Name>Nari</Name>
<Sal>7400</Sal>
</Emp>
<Emp>
<Name>Hari</Name>
<Sal>9000</Sal>
</Emp>
<Emp>
<Name>Suri</Name>
<Sal>8900</Sal>
</Emp>
</EmpCollection>
the wanted, correct result is produced:
32300
II. A more general solution is to use the FXSL 2.x f:foldl() function like this:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/" exclude-result-prefixes="f">
<xsl:import href="../f/func-foldl.xsl"/>
<xsl:import href="../f/func-Operators.xsl"/>
<xsl:output encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:value-of select="f:foldl(f:add(), 0, /*/*/Sal)"/>
</xsl:template>
</xsl:stylesheet>
When this XSLT 2.0 transformation is applied on the same XML document (above), the same correct and wanted result is produced:
32300
You can pass any two-argument function as argument to f:foldl() and produce the solutions of different problems.
For example, passing f:mult() instead of f:add() (and changing the "zero" argument to 1) produces the product of the salaries:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/" exclude-result-prefixes="f">
<xsl:import href="../f/func-foldl.xsl"/>
<xsl:import href="../f/func-Operators.xsl"/>
<xsl:output encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:value-of select="f:foldl(f:mult(), 1, /*/*/Sal)"/>
</xsl:template>
</xsl:stylesheet>
The result of applying this transformation to the same XML document is now the product of all Sal elements:
4.14918E15
III. In XSLT 3.0 (XPath 3.0) one can use the standard fold-left() or fold-right() function in exactly the same way as in the previous solution.
There are probably more elegant ways (and I guess sum() :) but the following works, by 'folding' siblings with addition. The termination condition is the last sibling (i.e. with no following nodes)
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/EmpCollection">
<result>Result Is : <xsl:apply-templates select="Emp[1]"/>
</result>
</xsl:template>
<xsl:template match="Emp[following-sibling::Emp]">
<xsl:variable name="salThis">
<xsl:value-of select="Sal"/>
</xsl:variable>
<xsl:variable name="salOthers">
<xsl:apply-templates select="following-sibling::Emp[position()=1]"/>
</xsl:variable>
<xsl:value-of select="$salThis + $salOthers"/>
</xsl:template>
<!--Termination - the last employee just returns its salary value-->
<xsl:template match="Emp[not(following-sibling::Emp)]">
<xsl:value-of select="Sal"/>
</xsl:template>
</xsl:stylesheet>
Gives the Result:
<result>Result Is : 32300</result>
Edit
After reading your question again, I guess this isn't a code golf question?
The below is a more simple way to do this with hard coded element names, which instructs the processor to add together the salaries of the named employees:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/EmpCollection">
<result>
Result Is : <xsl:value-of select="Emp[Name='Sai']/Sal + Emp[Name='Nari']/Sal + Emp[Name='Hari']/Sal + Emp[Name='Suri']/Sal"/>
</result>
</xsl:template>
</xsl:stylesheet>
which should then lead easily to understand what sum() is doing:
<xsl:template match="/EmpCollection">
<result>
Result Is : <xsl:value-of select="sum(Emp/Sal)"/>
</result>
</xsl:template>
Edit
Count is simply:
<xsl:template match="/EmpCollection">
<result>
<xsl:text>Count Is : </xsl:text>
<xsl:value-of select="count(Emp)"/>
</result>
</xsl:template>
Doing this without count() isn't as simple. Note that xsl:variables cannot be changed once assigned (immutable). So the following doesn't work at all.
<xsl:template match="/EmpCollection">
<xsl:variable name="count" select="0" />
<xsl:for-each select="Emp">
<!--NB : You can't do this. Variables are immutable - XSLT is functional -->
<xsl:variable name="count" select="$count + 1"></xsl:variable>
</xsl:for-each>
<result>
Result Is : <xsl:value-of select="$count"/>
</result>
</xsl:template>
So you could 'loop' through all the nodes, and then use position() to determine if this is the last node in the sequence. (Note that we've used for-each, but in practice it is better to use use apply-template)
<xsl:template match="/EmpCollection">
<result>
<xsl:text>Count Is : </xsl:text>
<xsl:for-each select="Emp">
<xsl:if test="position() = last()">
<xsl:number value="position()" format="1" />
</xsl:if>
</xsl:for-each>
</result>
</xsl:template>
:(
Related
I am new in XSLT and looking for help to remove duplicates of <EMP> from an xml document on the basis of their children's combined value. From each group of elements with the same value for this, the one with highest value for AIB_Position/AIB must be output. Below is my sample xml document and the corresponding desired output.
<Row_entry>
<Employees>
<Emp>
<Emp_id>E1</Emp_id>
<Emp_Name>Name1</Emp_Name>
<Country>C1</Country>
<AIB_Position>
<AIB>1500</AIB>
</AIB_Position>
</Emp>
<Emp>
<Emp_id>E2</Emp_id>
<Emp_Name>Name2</Emp_Name>
<Country>C2</Country>
<AIB_Position>
<AIB>1700</AIB>
</AIB_Position>
</Emp>
<Emp>
<Emp_id>E2</Emp_id>
<Emp_Name>Name2</Emp_Name>
<Country>C2</Country>
<AIB_Position>
<AIB>1800</AIB>
</AIB_Position>
</Emp>
</Employees>
</Row_entry>
Desired output(Removed duplicate Emp elements based on the combined <Emp_id>, <Emp_Name>, <Country> value):
<Row_entry>
<Employees>
<Emp>
<Emp_id>E1</Emp_id>
<Emp_Name>Name1</Emp_Name>
<Country>C1</Country>
<AIB_Position>
<AIB>1500</AIB>
</AIB_Position>
</Emp>
<Emp>
<Emp_id>E2</Emp_id>
<Emp_Name>Name2</Emp_Name>
<Country>C2</Country>
<AIB_Position>
<AIB>1800</AIB>
</AIB_Position>
</Emp>
</Employees>
</Row_entry>
I think you want this (directly using the XPath 2.0 max() function):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:template match="Employees">
<xsl:copy>
<xsl:for-each-group select="Emp" group-by="concat(Emp_id, '+', Emp_Name, '+', Country)">
<xsl:copy-of select="current-group()
[AIB_Position/AIB/number() = max(current-group()/AIB_Position/AIB/number())][1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
And if you suspect your XSLT processor of idiocy, such as calculating the max() more than once, use this more precisely directing transformation:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes"/>
<xsl:template match="Employees">
<xsl:copy>
<xsl:for-each-group select="Emp"
group-by="concat(Emp_id, '+', Emp_Name, '+', Country)">
<xsl:copy-of select=
"for $max in max(current-group()/AIB_Position/AIB/number())
return
current-group()[AIB_Position/AIB/number() = $max][1]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
In XSLT 2 or later, use for-each-group, for instance in XSLT 3 with a composite grouping key, then sort each group and output the maximum value:
<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:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="Employees">
<xsl:copy>
<xsl:for-each-group select="Emp" composite="yes" group-by="Emp_id, Emp_Name, Country">
<xsl:for-each select="current-group()">
<xsl:sort select="AIB_Position/AIB" order="descending"/>
<xsl:if test="position() = 1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With an XSLT 3 processor supporting the higher order sort function you could shorten that code to use
<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:output indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="Employees">
<xsl:copy>
<xsl:for-each-group select="Emp" composite="yes" group-by="Emp_id, Emp_Name, Country">
<xsl:sequence select="sort(current-group(), (), function($emp) { xs:integer($emp/AIB_Position/AIB) })[last()]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://stackoverflow.com/tags/xslt-grouping/info has some details on how to implement a composite grouping key of XSLT 3 in XSLT 2 by string-joining the components of the key, if you are limited to XSLT 2.
My knowledge to XSLT is limited but always eager to learn. I am currently working on a template that requires to transform the XML input. I've been trying to group the InvoiceNum fields and not getting anywhere.
I am getting an error: Envision.Utilities.XsltEngine-Object reference not set to an instance of an object.
Here's the input XML for reference:
<?xml version='1.0' ?>
<Request>
<Information>
<ImageID>987456321</ImageID>
<Contract>123456789</Contract>
<Lastname>MICKEYMOUSE</Lastname>
</Information>
<Document>
<InvoiceNum>123456823</InvoiceNum>
<Reference>AD20985224</Reference>
<InvoiceNum>100000123</InvoiceNum>
<Reference>AS20101387</Reference>
<InvoiceNum>858511825</InvoiceNum>
<Reference>GF96844</Reference>
<InvoiceNum>885154145</InvoiceNum>
<Reference>FGFD2018</Reference>
<InvoiceNum>25241111</InvoiceNum>
<Reference>SD88888</Reference>
<InvoiceNum>8571414</InvoiceNum>
<Reference>DF864841254</Reference>
</Document>
</Request>
Here's my XSLT format for reference:
What am I missing? Is there better way to format the XSLT template I have currently below? Any help is greatly appreciated.
<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:strip-space elements="*"/>
<xsl:param name="cols" select="3" />
<xsl:template match="Request">
<table border="1">
<xsl:apply-templates select="InvoiceNum[position() mod $cols = 1]"/>
</table>
</xsl:template>
<xsl:template match="Document">
<xsl:variable name="group" select=". | following-sibling::InvoiceNum
[position() < $cols]" />
<xsl:for-each select="*">
<xsl:variable name="i" select="position()" />
<Invoice>
<InvoiceNumber>
<xsl:value-of select="InvoiceNum()"/>
</InvoiceNumber>
<xsl:for-each select="$group">
<xsl:value-of select="*[$i]"/>
</xsl:for-each>
</Invoice>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Here's the XML output I'd like to have:
<InvCase>
<Invoices>
<InvoicemNumber>InvoiceNum1</InvoicemNumber>
<InvoicemNumber>InvoiceNum2</InvoicemNumber>
<InvoicemNumber>InvoiceNum3</InvoicemNumber>
</Invoices>
</InvCase>
It looks like you are trying to group in the InvoiceNum into groups of 3. The first issue you have is that in your template matching Request you do this...
<xsl:apply-templates select="InvoiceNum[position() mod $cols = 1]"/>
But InvoiceNum is not a child of Request, and so that selects nothing. You probably need to do this...
Additionally, you have a template matching Document, but this probably needs to match InvoiceNum (Doing following-sibling::InvoiceNum would not return anything if you were matching Document as the InvoiceNum elements are children of Document not following siblings).
Try this XSLT
<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:strip-space elements="*"/>
<xsl:param name="cols" select="3" />
<xsl:template match="Request">
<InvCases>
<xsl:apply-templates select="Document/InvoiceNum[position() mod $cols = 1]"/>
</InvCases>
</xsl:template>
<xsl:template match="InvoiceNum">
<xsl:variable name="group" select=". | following-sibling::InvoiceNum[position() < $cols]" />
<Invoice>
<xsl:for-each select="$group">
<InvoiceNumber>
<xsl:value-of select="."/>
</InvoiceNumber>
</xsl:for-each>
</Invoice>
</xsl:template>
</xsl:stylesheet>
I have got a source XML
<Records>
<Data>
<RecordType>New</RecordType>
<Number>4734122946</Number>
<DateOfBirth>20160506</DateOfBirth>
<Title>Mr</Title>
<ChangeTimeStamp>20160101010001</ChangeTimeStamp>
<SerialChangeNumber>01</SerialChangeNumber>
</Data>
<Data>
<RecordType>New</RecordType>
<Number>4734122946</Number>
<LastName>Potter</LastName>
<DateOfBirth>20160506</DateOfBirth>
<ChangeTimeStamp>20160101010002</ChangeTimeStamp>
<SerialChangeNumber>01</SerialChangeNumber>
</Data>
</Records>
I want to use XSLT 1.0 to produce the below output
<Contact>
<Number>4734122946</Number>
<Title>Mr</Title>
<LastName>Potter</LastName>
<BirthDate>20160506</BirthDate>
<ChangeTimeStamp>20160101010002</ChangeTimeStamp>
</Contact>
The XSLT has to group and merge the child nodes of Data records into one based on the Number field. Also if there are same elements present, then it should use the ChangeTimeStamp element to figure out the latest change and use that element.
I tried the below XSLT. But I am nowhere close to the output.
<?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="xml" indent="yes" />
<xsl:key name="groups" match="Data" use="Number"/>
<xsl:key name="sortGroup" match="Data" use ="ChangeTimeStamp"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Records">
<xsl:for-each select="Data[generate-id() = generate-id(key('groups',Number))]">
<Contact>
<Number>
<xsl:value-of select="Number"/>
</Number>
<xsl:for-each select="key('groups',Number)">
<xsl:for-each select="key('sortGroup',ChangeTimeStamp)">
<xsl:sort select="sortGroup" order="ascending"/>
<xsl:if test="Title">
<Title>
<xsl:value-of select ="Title"/>
</Title>
</xsl:if>
<xsl:if test="LastName">
<LastName>
<xsl:value-of select="LastName"/>
</LastName>
</xsl:if>
<BirthDate>
<xsl:value-of select="DateOfBirth"/>
</BirthDate>
</xsl:for-each>
</xsl:for-each>
</Contact>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Appreciate your help.
The XSLT has to group and merge the child nodes of Data records into
one based on the Number field. Also if there are same elements
present, then it should use the ChangeTimeStamp element to figure out
the latest change and use that element.
For that, I believe you would want to do something like:
<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:key name="group" match="Data" use="Number"/>
<xsl:key name="item" match="Data/*" use="concat(../Number, '|', name())"/>
<xsl:template match="/Records">
<root>
<xsl:for-each select="Data[generate-id() = generate-id(key('group', Number)[1])]">
<Contact>
<xsl:for-each select="key('group', Number)/*[generate-id() = generate-id(key('item', concat(../Number, '|', name()))[1])]">
<xsl:for-each select="key('item', concat(../Number, '|', name()))">
<xsl:sort select="../ChangeTimeStamp" data-type="number" order="descending"/>
<xsl:if test="position()=1">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</Contact>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
You will have to make some adjustments if you want to include only some data items and/or if you want them to appear in particular order. If you have a list of all possible data item names (e.g. Title, LastName, DateOfBirth, etc.) then this could be simpler.
i am trying to paas the dynamic parameter while calling the template to suppress nodes from the xml.
I would call this template like:
transform employee.xml suppress.xsl ElementsToSuppress=id,fname
employee.xml
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<id>1</id>
<firstname>xyz</firstname>
<lastname>abc</lastname>
<age>32</age>
<department>xyz</department>
</Employee>
<Employee>
<id>2</id>
<firstname>XY</firstname>
<lastname>Z</lastname>
<age>21</age>
<department>xyz</department>
</Employee>
</Employees>
Suppress.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:elements="http://localhost">
<elements:name abbrev="id">id</elements:name>
<elements:name abbrev="fname">firstname</elements:name>
<xsl:param name="ElementsToSuppress" ></xsl:param>
<xsl:variable name="tokenizedSample" select="tokenize($ElementsToSuppress,',')"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
<xsl:for-each select="$tokenizedSample">
<xsl:call-template name ="Suppress" >
<xsl:with-param name="parElementName">
<xsl:value-of select="."/>
</xsl:with-param>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template name="Suppress">
<xsl:param name="parElementName" select="''"></xsl:param>
<xsl:variable name="extNode" select="document('')/*/elements:name[#abbrev=$parElementName]"/>
<xsl:call-template name="test" >
<xsl:with-param name="parElementName" >
<xsl:value-of select="$extNode"/>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="test" match="*[name() = $parElementName]" >
<xsl:param name="parElementName" select="''"></xsl:param>
<xsl:call-template name="SuppressElement" />
</xsl:template>
<xsl:template name="SuppressElement" />
</xsl:stylesheet>
Can we achieve output by using this or some other way? The ideal way is to pass the comma separated abbreviations of nodes and suppress them in one call.
Any help will be appreciated.
Regards,
AB
You don't need XSLT 2.0 for this processing.
This XSLT 1.0 transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pNodesToSuppress"
select="'id,fname,pi'"/>
<my:toSuppress>
<name abbrev="id">id</name>
<name abbrev="fname">firstname</name>
<name abbrev="pi">somePI</name>
</my:toSuppress>
<xsl:variable name="vToSuppressTable" select=
"document('')/*/my:toSuppress/*"/>
<xsl:variable name="vNamesToSupress">
<xsl:for-each select=
"$vToSuppressTable
[contains(concat(',',$pNodesToSuppress,','),
concat(',',#abbrev, ',')
)
]">
<xsl:value-of select="concat(',',.)"/>
</xsl:for-each>
<xsl:text>,</xsl:text>
</xsl:variable>
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()" priority="0.01">
<xsl:if test=
"not(contains($vNamesToSupress,
concat(',',name(),',')
)
)">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document (with an added processing instruction to demonstrate how we can also delete PIs -- not only elements):
<Employees>
<Employee>
<id>1</id>
<firstname>xyz</firstname>
<lastname>abc</lastname>
<age>32</age>
<department>xyz</department>
</Employee>
<?somePI This PI will be deleted ?>
<Employee>
<id>2</id>
<firstname>XY</firstname>
<lastname>Z</lastname>
<age>21</age>
<department>xyz</department>
</Employee>
</Employees>
produces the wanted, correct results:
<Employees>
<Employee>
<lastname>abc</lastname>
<age>32</age>
<department>xyz</department>
</Employee>
<Employee>
<lastname>Z</lastname>
<age>21</age>
<department>xyz</department>
</Employee>
</Employees>
Do note:
This is a pure XSLT 1.0 transformation.
2.Not only elements, but also processing instructions are deleted, when their name is specified via the external parameter $pNodesToSuppress.
I don't get why the parameter value "fname" would suppress an element called "firstname" but apart from that you might simply want
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="ElementsToSuppress" as="xs:string" select="'id,firstname'"/>
<xsl:variable name="names-to-suppress" as="xs:QName*"
select="for $s in tokenize($ElementsToSuppress, ',') return QName('', $s)"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[node-name(.) = $names-to-suppress]"/>
</xsl:stylesheet>
[edit]
I missed that your sample stylesheet seemed to contain a mapping from those parameters to the elements names in the sample input. In that case here is an adapted version of the stylesheet to handle that case:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:data="http://example.com/data"
xmlns:elements="http://example.com/elements"
exclude-result-prefixes="xs data elements"
version="2.0">
<data:data>
<elements:name abbrev="id">id</elements:name>
<elements:name abbrev="fname">firstname</elements:name>
</data:data>
<xsl:key name="e-by-abbrev" match="elements:name" use="#abbrev"/>
<xsl:param name="ElementsToSuppress" as="xs:string" select="'id,fname'"/>
<xsl:variable name="names-to-suppress" as="xs:QName*"
select="for $s in tokenize($ElementsToSuppress, ',') return QName('', key('e-by-abbrev', $s, document('')/xsl:stylesheet/data:data))"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[node-name(.) = $names-to-suppress]"/>
</xsl:stylesheet>
[second edit to implement further request] Here is a sample that also checks the relationship attribute values:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:data="http://example.com/data"
xmlns:elements="http://example.com/elements"
exclude-result-prefixes="xs data elements"
version="2.0">
<xsl:param name="ElementsToSuppress" as="xs:string" select="'id'"/>
<xsl:param name="parVAObjectRelationship" as="xs:string" select="'a,b'"/>
<xsl:variable name="names-to-suppress" as="xs:QName*"
select="for $s in tokenize($ElementsToSuppress, ',') return QName('', key('e-by-abbrev', $s, document('')/xsl:stylesheet/data:data))"/>
<xsl:variable name="att-values-to-suppress" as="xs:string*"
select="tokenize($parVAObjectRelationship, ',')"/>
<data:data>
<elements:name abbrev="id">id</elements:name>
<elements:name abbrev="fname">firstname</elements:name>
</data:data>
<xsl:key name="e-by-abbrev" match="elements:name" use="#abbrev"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[#relationship = $att-values-to-suppress]"/>
<xsl:template match="*[node-name(.) = $names-to-suppress]"/>
</xsl:stylesheet>
When applied to
<Employees>
<Employee>
<id>1</id>
<firstname>xyz</firstname>
<lastname relationship="a">abc</lastname>
<age relationship="b">32</age>
<department>xyz</department>
</Employee>
</Employees>
the output is
<Employees>
<Employee>
<firstname>xyz</firstname>
<department>xyz</department>
</Employee>
</Employees>
You might want to add strip-space and output indent="yes" to prevent the blank lines.
If you are using the old Saxon-B or newer Saxon-PE or Saxon-EE as XSLT processor, you can use a saxon extension to achieve dynamic template calls:
<saxon:call-template name="{$templateName}"/>
Don't forget to declare the saxon-Namespace in xsl-stylesheet element:
<xsl:stylesheet xmlns:saxon="http://saxon.sf.net/" [...] >
I have a list of items:
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
and I want as output
z
c
x
a
I have no order information in the file and I just want to reverse the lines. The last line in the source file should be first line in the output. How can I solve this problem with XSLT without sorting by the content of the items, which would give the wrong result?
I will present two XSLT solutions:
I. XSLT 1.0 with recursion Note that this solution works for any node-set, not only in the case when the nodes are siblings:
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:call-template name="reverse">
<xsl:with-param name="pList" select="*"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="pList"/>
<xsl:if test="$pList">
<xsl:value-of
select="concat($pList[last()], '
')"/>
<xsl:call-template name="reverse">
<xsl:with-param name="pList"
select="$pList[not(position() = last())]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on this XML document:
<t>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</t>
produces the wanted result:
z
c
x
a
II. XSLT 2.0 solution :
<xsl:stylesheet version="2.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="reverse(*)/string(.)"
separator="
"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document, the same correct result is produced.
XML CODE:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<device>
<element>a</element>
<element>x</element>
<element>c</element>
<element>z</element>
</device>
XSLT CODE:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//device">
<xsl:for-each select="element">
<xsl:sort select="position()" data-type="number" order="descending"/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
note: if you're using data-type="number", and any of the values aren't numbers, those non-numeric values will sort before the numeric values. That means if you're using order="ascending", the non-numeric values appear first; if you use order="descending", the non-numeric values appear last.
Notice that the non-numeric values were not sorted; they simply appear in the output document in the order in which they were encountered.
also, you may find usefull to read this:
http://docstore.mik.ua/orelly/xml/xslt/ch06_01.htm
Not sure what the full XML looks like, so I wrapped in a <doc> element to make it well formed:
<doc>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</doc>
Running that example XML against this stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:call-template name="reverse">
<xsl:with-param name="item" select="doc/item[position()=last()]" />
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="item" />
<xsl:value-of select="$item" />
<!--Adds a line feed-->
<xsl:text>
</xsl:text>
<!--Move on to the next item, if we aren't at the first-->
<xsl:if test="$item/preceding-sibling::item">
<xsl:call-template name="reverse">
<xsl:with-param name="item" select="$item/preceding-sibling::item[1]" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Produces the requested output:
z
c
x
a
You may need to adjust the xpath to match your actual XML.
Consider this XML input:
<?xml version="1.0" encoding="utf-8" ?>
<items>
<item>a</item>
<item>x</item>
<item>c</item>
<item>z</item>
</items>
The XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/items[1]">
<xsl:variable name="items-list" select="." />
<xsl:variable name="items-count" select="count($items-list/*)" />
<xsl:for-each select="item">
<xsl:variable name="index" select="$items-count+1 - position()"/>
<xsl:value-of select="$items-list/item[$index]"/>
<xsl:value-of select="'
'"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And the result:
z
c
x
a