How to sort the 'joined' xsl nodeset - sorting

I have this (simplified) XML
<?xml version="1.0" encoding="iso-8859-1"?>
<LENEX version="3.0">
<MEETS>
<MEET name="British Gas Champs 2012">
<SESSIONS>
<SESSION number="1" name="Session 1" course="LCM" date="2012-07-22">
<EVENTS>
<EVENT eventid="104" number="104" gender="M" round="PRE" order="4">
<SWIMSTYLE distance="100" stroke="BACK" name="Boys 14 Yrs 100m Backstroke" />
<AGEGROUPS>
<AGEGROUP agegroupid="1" name="14 Yrs Age Group">
<RANKINGS>
<RANKING place="3" resultid="1" />
</RANKINGS>
</AGEGROUP>
</AGEGROUPS>
</EVENT>
</EVENTS>
</SESSION>
<SESSION number="2" name="Session 2" course="LCM" date="2012-07-22">
<EVENTS>
<EVENT eventid="207" number="207" gender="M" round="PRE" order="7">
<SWIMSTYLE distance="100" stroke="FREE" name="Boys 14 Yrs 100m Freestyle"/>
<AGEGROUPS>
<AGEGROUP agegroupid="1" name="14 Yrs Age Group">
<RANKINGS>
<RANKING place="1" resultid="2"/>
</RANKINGS>
</AGEGROUP>
</AGEGROUPS>
</EVENT>
</EVENTS>
</SESSION>
</SESSIONS>
<CLUBS>
<CLUB name="Aberdeen ASC" region="X" type="CLUB">
<ATHLETES>
<ATHLETE athleteid="1169" lastname="Butt" firstname="Suleman">
<RESULTS>
<RESULT resultid="1" eventid="104" swimtime="00:01:01.18"/>
<RESULT resultid="2" eventid="207" swimtime="00:00:53.06"/>
</RESULTS>
</ATHLETE>
</ATHLETES>
</CLUB>
</CLUBS>
</MEET>
</MEETS>
</LENEX>
and this stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl">
<xsl:variable name="session" select="/LENEX/MEETS/MEET/SESSIONS/SESSION" />
<xsl:variable name="athlete" select="/LENEX/MEETS/MEET/CLUBS/CLUB/ATHLETES/ATHLETE" />
<!-- variable for testing/debugging-->
<xsl:variable name="athlno" select="1169" />
<!--<xsl:param name="evno" />-->
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates select="$athlete" />
</body>
</html>
</xsl:template>
<!-- find the results (#resultid) for athleteid in $athlno -->
<xsl:template match="ATHLETE[#athleteid]">
<xsl:choose>
<xsl:when test="self::ATHLETE[#athleteid = $athlno]/RESULTS">
<xsl:apply-templates select="child::RESULTS/RESULT">
<!-- these 'sorts' works -->
<!-- <xsl:sort select="#resultid" data-type="text" order="descending"/>
<xsl:sort select="#eventid" order="descending"/> -->
</xsl:apply-templates>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- create $res1 $stim1 variables to pass to 'SESSIONS' node -->
<xsl:template match="RESULT">
<xsl:variable name="res1" ><xsl:value-of select="#resultid"/></xsl:variable>
<xsl:variable name="stim1" ><xsl:value-of select="#swimtime"/></xsl:variable>
<tr>
<!--join to 'SESSIONS' node -->
<xsl:apply-templates select="$session/EVENTS/EVENT">
<!-- this xsl:sort doesn't work
<xsl:sort select="#number" data-type="text" order="descending"/>-->
<xsl:with-param name="res2" select="$res1"/>
<xsl:with-param name="stim2" select="$stim1"/>
</xsl:apply-templates>
</tr>
</xsl:template>
<xsl:template match="EVENT">
<xsl:param name="res2" />
<xsl:param name="stim2" />
<xsl:for-each select="child::AGEGROUPS/AGEGROUP/RANKINGS/RANKING[#resultid = $res2]">
<!-- this xsl:sort doesn't work -->
<xsl:sort select="#place" data-type="text" order="ascending"/>
<xsl:value-of select="#place"/>
<xsl:choose>
<xsl:when test="../../../../#round = 'TIM'">
<xsl:text> </xsl:text>HDW
</xsl:when>
<xsl:when test="../../../../#round = 'PRE'">
<xsl:text> </xsl:text>Heat
</xsl:when>
<xsl:when test="../../../../#round = 'SEM'">
<xsl:text> </xsl:text>Semi-F
</xsl:when>
<xsl:when test="../../../../#round = 'FIN'">
<xsl:text> </xsl:text>Final
</xsl:when>
<xsl:otherwise>
<xsl:text> </xsl:text>n/a
</xsl:otherwise>
</xsl:choose>
<xsl:text> </xsl:text><xsl:value-of select="../../../../SWIMSTYLE/#name" />
<xsl:text> </xsl:text><xsl:value-of select="substring($stim2,4)"/>
<xsl:text> </xsl:text><xsl:value-of select="../../../../#number" /><br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I only seem to be able to sort by the main nodeset ie RESULTS/RESULT/#resultid or RESULTS/RESULT/eventid. I would like to sort the results of a 'join' by EVENT/SWIMSTYLE/#stroke, EVENT/SWIMSTYLE/#distance and EVENT/#round. I am limited to XSLT 1.0. What I've tried is commented in the XSLT above.
The output I'm looking for is ...
3 Final Boys 14 Yrs 100m Backstroke 01:00.83 269
3 Heat Boys 14 Yrs 100m Backstroke 01:01.18 104
3 Semi-F Boys 14 Yrs 100m Backstroke 01:00.66 156
1 Final Boys 14 Yrs 100m Freestyle 00:52.61 363
1 Heat Boys 14 Yrs 100m Freestyle 00:53.06 207
1 Semi-F Boys 14 Yrs 100m Freestyle 00:53.56 259
1 Final Boys 14 Yrs 200m Freestyle 01:53.34 555
1 Heat Boys 14 Yrs 200m Freestyle 01:57.29 402
1 Semi-F Boys 14 Yrs 200m Freestyle 01:55.23 454
1 Final Boys 14 Yrs 400m Freestyle 04:03.46 351
1 Heat Boys 11/14 Yrs 400m Freestyle 04:09.46 301

A few keys will help you build paths to your sort attributes:
<!-- Map from #resultid to EVENT -->
<xsl:key name="result-event" match="//EVENT" use="descendant::RANKING/#resultid"/>
<!-- Map from #resultid to RANKING -->
<xsl:key name="ranking" match="//RANKING" use="#resultid"/>
Then, when templating the RESULT elements for an ATHLETE, you can reference the sort attributes:
<xsl:template match="/">
<xsl:apply-templates select="//ATHLETE"/>
</xsl:template>
<xsl:template match="ATHLETE">
<xsl:apply-templates select="RESULTS/RESULT">
<xsl:sort select="key('result-event', #resultid)/SWIMSTYLE/#stroke"/>
<xsl:sort select="key('result-event', #resultid)/SWIMSTYLE/#distance" data-type="number"/>
<xsl:sort select="key('result-event', #resultid)/#round"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="RESULT">
<xsl:value-of select="key('ranking', #resultid)/#place"/>
<xsl:apply-templates select="key('result-event', #resultid)"/>
<xsl:value-of select="#swimtime"/>
<br/>
</xsl:template>
<xsl:template match="EVENT">
<xsl:text> </xsl:text><xsl:value-of select="#round"/>
<xsl:text> </xsl:text><xsl:value-of select="SWIMSTYLE/#name" />
<xsl:text> </xsl:text><xsl:value-of select="../../#number" />
</xsl:template>
You can adjust the sort order for rounds by using a lookup table to map the #round key to an arbitrary surrogate key.

Related

Use xmlstarlet to add missing elements?

I have a number of vendor records which contain multiple addresses e.g.
<vendor>
<addresses>
<address primary="yes">
<line1 />
<city />
<state />
....
</address>
<address primary="no">
<line1 />
<city />
<state />
....
</address>
</addresses>
</vendor>
Some required elements are missing -- preventing updating of the records. Can xmlstarlet can be used to add an element with a default value if it is missing?
Here's a simple example. I'll use xmllint --auto for the xml source. Then we'll add an <add-me> element as a child of <info> if it doesn't exist using the identity transform pattern.
Source xml:
xmllint --auto
<?xml version="1.0"?>
<info>abc</info>
Add the missing element:
xmllint --auto | xsltproc add-missing.xsl -
<?xml version="1.0"?>
<info><add-me>some stuff</add-me>abc</info>
add-missing.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="info">
<xsl:copy>
<xsl:if test="not(add-me)">
<add-me>some stuff</add-me>
</xsl:if>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Another XSLT w/xmlstarlet option is to use a variable that contains the required elements (with or without default values) and treat is as a node set (using the supported exsl:node-set() function).
You can then iterate over the node set to see if an element with the same name already exists. If it does, use it. Otherwise use the default.
Example...
XML Input (input.xml)
<vendor>
<addresses>
<address primary="yes">
<line1>address 1 line1</line1>
<state>address 1 state1</state>
</address>
<address primary="no">
<line1>address 2 line1</line1>
<city>address 2 city</city>
<state>address 2 state</state>
</address>
</addresses>
</vendor>
XSLT 1.0 (so.xsl)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="exsl">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="req_elems">
<req>
<line1/>
<city/>
<state/>
<country/>
</req>
</xsl:variable>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="address">
<xsl:variable name="ctx" select="."/>
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each select="exsl:node-set($req_elems)/req/*">
<xsl:choose>
<xsl:when test="$ctx/*[local-name()=local-name(current())]">
<xsl:apply-templates select="$ctx/*[local-name()=local-name(current())]"/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
XML Output
<vendor>
<addresses>
<address primary="yes">
<line1>address 1 line1</line1>
<city/>
<state>address 1 state1</state>
<country/>
</address>
<address primary="no">
<line1>address 2 line1</line1>
<city>address 2 city</city>
<state>address 2 state</state>
<country/>
</address>
</addresses>
</vendor>
Note: This only works if the only allowed elements in address are the same as $req_elements. For example, if you have an element named "foo" in address, it will be dropped from the output.

XSLT: sort and limit nested nodes

XML:
<node>
<node date="01-01-2002">Node</node>
<node date="01-01-2005">Node</node>
<node date="01-01-2001">Node</node>
<node date="01-01-2003">Node</node>
<node date="01-01-2006">Node</node>
<node>
<node date="01-01-2000">Node</node>
<node date="01-01-2007">Node</node>
</node>
<node date="01-01-2004">Node</node>
</node>
Problem:
I need to sort by date AND take a limited number of sorted nodes. Need to be able to traverse any number of levels.
Required result:
<p>01-01-2000</p>
<p>01-01-2001</p>
<p>01-01-2002</p>
<p>01-01-2003</p>
<p>01-01-2004</p>
Assumptions:
For sorting by date I use c# extension method that returns time stamp:
<xsl:sort select="cs:formatDate(#date)" order="ascending" data-type="number" />
Limit to 5 oldest nodes.
Order: ascending
XSLT 1.0
EDIT:
As requested this is where i got so far:
I can do sorting and limiting for not nested nodes:
<xsl:template match="node">
<xsl:apply-templates select="node">
<xsl:sort select="cs:formatDate(#date,'dd-MM-yyyy','timestamp')" order="ascending" data-type="number" />
<xsl:with-param name="limit" select="5"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="node[#date]">
<xsl:param name="limit" />
<xsl:if test="position() < $limit+1">
<h5><xsl:value-of select="#date"/></h5>
</xsl:if>
</xsl:template>
Or when I try to apply for nested as below, I get nested nodes sorted in isolation, and I cannot limit them in same way anymore:
<xsl:template match="*">
<xsl:apply-templates select="node[#date]">
<xsl:sort select="cs:formatDate(#date,'dd-MM-yyyy','timestamp')" order="ascending" data-type="number" />
</xsl:apply-templates>
<xsl:apply-templates select="node[not(#date)]">
</xsl:apply-templates>
</xsl:template>
<xsl:template match="node[#date]">
<h5><xsl:value-of select="#date"/></h5>
</xsl:template>
<xsl:template match="node[not(#date)]">
<xsl:apply-templates select="node[#date]">
<xsl:sort select="cs:formatDate(#date,'dd-MM-yyyy','timestamp')" order="ascending" data-type="number" />
</xsl:apply-templates>
<xsl:apply-templates select="node[not(#date)]">
</xsl:apply-templates>
</xsl:template>
EDIT:
I thought it is obvious, but probably not: I need sort to be applied before the limit. E.g: "get oldest five" and NOT:"get first five nodes from xml and then sort them"
<xsl:template match="/">
<xsl:apply-templates select="//node[#date]">
<xsl:sort select="concat(substring-after(substring-after(#date,'-'),'-'),substring-before(substring-after(#date,'-'),'-'),substring-before(#date,'-'))" order="ascending" data-type="number" />
<xsl:with-param name="start" select="1"/>
<xsl:with-param name="end" select="5"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="node">
<xsl:param name="start" />
<xsl:param name="end" />
<xsl:if test="position() >= $start and position() <= $end">
<p>
<xsl:value-of select="#date"/>
</p>
</xsl:if>
</xsl:template>

xslt 1.0 to replace xpath references with the actual data in the same xml

i have some requirement, where i need to replace the references in the same xml file.
limitation is to use xslt 1.0 only.
below is my sample input xml.
<org>
<depts>
<dept>
<deptId>1009</deptId>
<deptName>IT</deptName>
<deptAccessCode>IT-1009</deptAccessCode>
</dept>
<dept>
<deptId>2344</deptId>
<deptName>BPO</deptName>
<deptAccessCode>BP-2344</deptAccessCode>
</dept>
</depts>
<employees>
<employee>
<name>abc</name>
<dept>
<REFERENCE>
<LocationXPath>/org/depts/dept[2]</LocationXPath>
</REFERENCE>
</dept>
<employee>
</employees>
</org>
now i want to replace the node REFERENCE with actual data at the XPath /org/depts/dept[2].
so the output xml should be like below.
<org>
<depts>
<dept>
<deptId>1009</deptId>
<deptName>IT</deptName>
<deptAccessCode>IT-1009</deptAccessCode>
</dept>
<dept>
<deptId>2344</deptId>
<deptName>BPO</deptName>
<deptAccessCode>BP-2344</deptAccessCode>
</dept>
</depts>
<employees>
<employee>
<name>abc</name>
<dept>
<deptId>2344</deptId>
<deptName>BPO</deptName>
<deptAccessCode>BP-2344</deptAccessCode>
</dept>
<employee>
</employees>
</org>
i have several REFERENCE nodes in different elements referencing to different xpaths across the xml tree, which i need to replace them with actual data.
<someWhereInTheXmlTree>
<sometag>
<REFERENCE>
<LocationXPath>some/reference[1]/to/a/node[3]/in/the[4]/same/xml</LocationXPath>
</REFERENCE>
</sometag>
<someWhereInTheXmlTree>
...
<ffff>
<bbbb>
<REFERENCE>
<LocationXPath>abc/xyz[1]/node[4]/element</LocationXPath>
</REFERENCE>
</bbbb>
<ffff>
please help me on this.
Thanks in advance for the help.
So far i have implemented one XSLT to replace the references but now i am facing unwanted empty name spaces.
Here is my XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions"
xmlns="http://www.realestate.org/residential/2010/schemas" >
<xsl:output omit-xml-declaration="no" indent="yes" method = "xml" />
<xsl:strip-space elements="*"/>
<xsl:param name="myxml" />
<xsl:template match="node()|#*">
<xsl:param name="isNodeToReplace"><xsl:call-template name="ReferenceCheck" /></xsl:param>
<xsl:choose>
<xsl:when test="$isNodeToReplace='true'">
<xsl:call-template name="replaceWithData">
<xsl:with-param name="ref"><xsl:value-of select="." /></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ReferenceCheck">
<xsl:choose>
<xsl:when test="name(child::*[1])='REFERENCE' and name(child::*[1]//child::*[1])='LocationXPath'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="replaceWithData">
<xsl:param name="ref" />
<xsl:copy-of select="tib:evaluate($myxml,$ref)" />
</xsl:template>
</xsl:stylesheet>
in the above XSLT i am passing the entire xml (same xml, which is being processed) as a parameter $myxml
Below is my sample input XML --this is just a snippet of xml,The actual xml file which i am dealing with is too large and contains so complex tree structure.How ever this sample xml is suffice enough to produce my problem.
Input file
<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
<depts>
<dept>
<deptId>1</deptId>
<deptName>health</deptName>
<deptAccessCode>HL007845</deptAccessCode>
</dept>
</depts>
<employees>
<employee>
<name>TOM</name>
<dept>
<REFERENCE>
<LocationXPath>/org/depts/dept[1]</LocationXPath>
</REFERENCE>
</dept>
</employee>
</employees>
</org>
my output file
<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
<depts>
<dept>
<deptId>1</deptId>
<deptName>health</deptName>
<deptAccessCode>HL007845</deptAccessCode>
</dept>
</depts>
<employees>
<employee>
<name>TOM</name>
<dept xmlns="">
<deptId>1</deptId>
<deptName>health</deptName>
<deptAccessCode>HL007845</deptAccessCode>
</dept>
</employee>
</employees>
</org>
Where as Expected output
<?xml version="1.0" encoding="UTF-8"?>
<org xmlns="http://www.realestate.org/residential/2010/schemas">
<depts>
<dept>
<deptId>1</deptId>
<deptName>health</deptName>
<deptAccessCode>HL007845</deptAccessCode>
</dept>
</depts>
<employees>
<employee>
<name>TOM</name>
<dept>
<deptId>1</deptId>
<deptName>health</deptName>
<deptAccessCode>HL007845</deptAccessCode>
</dept>
</employee>
</employees>
</org>
so i am getting unwanted empty name space in << dept xmlns="">> in the replaced root element.
Hope this could clearly explain my problem
Thanks in Advance
ultimately i have found the solution at the link below to remove the unwanted empty name spaces.
http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/0de59291-ef3a-4a4c-9ca5-17923b16a504
Here is the new XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:tib="http://www.tibco.com/bw/xslt/custom-functions"
xmlns="http://www.realestate.org/residential/2010/schemas" >
<xsl:output omit-xml-declaration="no" indent="yes" method = "xml" />
<xsl:strip-space elements="*"/>
<xsl:param name="myxml" />
<xsl:template match="node()|#*">
<xsl:param name="isNodeToReplace"><xsl:call-template name="ReferenceCheck" /></xsl:param>
<xsl:choose>
<xsl:when test="$isNodeToReplace='true'">
<xsl:call-template name="replaceWithData">
<xsl:with-param name="ref"><xsl:value-of select="." /></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="ReferenceCheck">
<xsl:choose>
<xsl:when test="name(child::*[1])='REFERENCE' and name(child::*[1]//child::*[1])='LocationXPath'">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="replaceWithData">
<xsl:param name="ref" />
<xsl:apply-templates select="tib:evaluate($myxml,$ref)" mode="move-to-namespace">
<xsl:with-param name="namespace" select="'http://www.realestate.org/residential/2010/schemas'" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="move-to-namespace">
<xsl:param name="namespace" />
<xsl:element name="{local-name()}" namespace="{$namespace}">
<xsl:copy-of select="#*" />
<xsl:apply-templates select="node()" mode="move-to-namespace">
<xsl:with-param name="namespace" select="$namespace"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="text() | comment() | processing-instruction()" mode="move-to-namespace">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
if any xslt expert refines it further to avoid any unnecessary instruction with proper explanation its very glad.
Thanks in advance
In general:
Not posiible in pure XSLT 1.0.
Not possible in pure XSLT 2.0
May be possible in pure XSLT 3.0 -- read about the
<xsl:evaluate> instruction.
In XSLT 1.0 you may be lucky if your XSLT processor implements the EXSLT dyn:evaluate() extension function (a few do).
Otherwize, you will have to write an extension function to select the
nodes and return them back.
If there are restrictions on the syntax of the XPath expressions, then it may be possible to implement a pure XSLT 1.0 solution.
This is not possible in pure XSLT 1.0. You have to use extension function, e.g. Xalan evaluate expression

Get full XPath from partial

I am using selenium with perl and have label on page, to access this label i have following xpath: //*[text()='some here'] , the problem that a need to get full xpath of this element, like /html/body/table/tr/..../any other/and other/ , is there is any selenium method or perl function ? looking for perl solution or any other working things.
thanks
looking for perl solution or any other
working things
This XPath 2.0 expression:
string-join(for $node in ancestor-or-self::node()
return concat(('#')[$node/self::attribute()],
$node/name(),
(concat('[',
count($node/preceding-sibling::node()
[name()=$node/name()]) + 1,
']'))[$node/../node()
[name()=$node/name()][2]]),
'/')
Edit: Shorter expression.
This XSLT 1.0 transformation produces an XPath expression for every node contained in the $pNode parameter:
<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="node()|#*">
<path>
<xsl:call-template name="buildPath"/>
</path>
<xsl:apply-templates select="node()|#*"/>
</xsl:template>
<xsl:template name="buildPath">
<xsl:variable name="pNode" select="."/>
<xsl:variable name="theResult">
<xsl:for-each select="$pNode">
<xsl:variable name="theNode" select="."/>
<xsl:for-each select=
"$theNode
|
$theNode/ancestor-or-self::node()[..]">
<xsl:element name="slash">/</xsl:element>
<xsl:choose>
<xsl:when test="self::*">
<xsl:element name="nodeName">
<xsl:value-of select="name()"/>
<xsl:variable name="thisPosition" select=
"count(preceding-sibling::*
[name(current())
=
name()])"/>
<xsl:variable name="numFollowing" select=
"count(following-sibling::
*[name(current())
=
name()])"/>
<xsl:if test="$thisPosition + $numFollowing > 0">
<xsl:value-of select=
"concat('[', $thisPosition +1, ']')"/>
</xsl:if>
</xsl:element>
</xsl:when>
<xsl:otherwise> <!-- This node is not an element -->
<xsl:choose>
<xsl:when test="count(. | ../#*) = count(../#*)">
<!-- Attribute -->
<xsl:element name="nodeName">
<xsl:value-of select="concat('#',name())"/>
</xsl:element>
</xsl:when>
<xsl:when test="self::text()"> <!-- Text -->
<xsl:element name="nodeName">
<xsl:value-of select="'text()'"/>
<xsl:variable name="thisPosition"
select="count(preceding-sibling::text())"/>
<xsl:variable name="numFollowing"
select="count(following-sibling::text())"/>
<xsl:if test="$thisPosition + $numFollowing > 0">
<xsl:value-of select=
"concat('[', $thisPosition +1, ']')"/>
</xsl:if>
</xsl:element>
</xsl:when>
<xsl:when test="self::processing-instruction()">
<!-- Processing Instruction -->
<xsl:element name="nodeName">
<xsl:value-of select="'processing-instruction()'"/>
<xsl:variable name="thisPosition"
select="count(preceding-sibling::processing-instruction())"/>
<xsl:variable name="numFollowing"
select="count(following-sibling::processing-instruction())"/>
<xsl:if test="$thisPosition + $numFollowing > 0">
<xsl:value-of select=
"concat('[', $thisPosition +1, ']')"/>
</xsl:if>
</xsl:element>
</xsl:when>
<xsl:when test="self::comment()"> <!-- Comment -->
<xsl:element name="nodeName">
<xsl:value-of select="'comment()'"/>
<xsl:variable name="thisPosition"
select="count(preceding-sibling::comment())"/>
<xsl:variable name="numFollowing"
select="count(following-sibling::comment())"/>
<xsl:if test="$thisPosition + $numFollowing > 0">
<xsl:value-of select=
"concat('[', $thisPosition +1, ']')"/>
</xsl:if>
</xsl:element>
</xsl:when>
<!-- Namespace: -->
<xsl:when test=
"count(. | ../namespace::*)
=
count(../namespace::*)">
<xsl:variable name="apos">'</xsl:variable>
<xsl:element name="nodeName">
<xsl:value-of select="concat('namespace::*',
'[local-name() = ', $apos, local-name(), $apos, ']')"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<!-- <xsl:text>
</xsl:text> -->
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$theResult"/>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document:
<div id="entry-1" class="item-asset asset hentry">
<div class="asset-header">
<h2 class="asset-name entry-title">
<a rel="bookmark" href="http://blahblah.com/paper-scissors">Paper Scissors</a>
</h2>
</div>
<div class="asset-content entry-content">
<div class="asset-body">
<p>Paper and scissors</p>
</div>
</div>
</div>
the result is a list of the XPath expressions for every node in the document:
<path>/div</path>
<path>/div/#id</path>
<path>/div/#class</path>
<path>/div/div[1]</path>
<path>/div/div[1]/#class</path>
<path>/div/div[1]/h2</path>
<path>/div/div[1]/h2/#class</path>
<path>/div/div[1]/h2/a</path>
<path>/div/div[1]/h2/a/#rel</path>
<path>/div/div[1]/h2/a/#href</path>
<path>/div/div[1]/h2/a/text()</path>
<path>/div/div[2]</path>
<path>/div/div[2]/#class</path>
<path>/div/div[2]/div</path>
<path>/div/div[2]/div/#class</path>
<path>/div/div[2]/div/p</path>
<path>/div/div[2]/div/p/text()</path>

Modify an XML node using [MS] XSLT Script

I would like to select a node and modify its attributes and child-nodes using an
xsl:script function. In addition, templates matching child-nodes of that node should
STILL perform their job (after script is done processing the node).
Can it be done using XSLT?
Can you please provide an example / skeleton for such a transformation?
Yes, it can be done. I don't seem to see what the problem is because the XML (or whatever output) of an XSL script is buffered independently from its input.
This is illustrated in the following example whereby a simple XSL script copies an input XML document mostly as-is, changing a few things:
the root element name and attribute
flattening by removing the element from the hierarchy
dropping the results/date element
rename the item's 'source' attribute 'origin'
change the item's 'level' attribute value
rename the FirstName and LastName elements of the item elements
Sample input
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyRoot version="1.2">
<results>
<info>Alpha Bravo</info>
<author>Employee No 321</author>
<date/>
<item source="www" level="6" cost="33">
<FirstName>Jack</FirstName>
<LastName>Frost</LastName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item source="db-11" level="1" cost="65" qry="routine 21">
<FirstName>Mike</FirstName>
<LastName>Black</LastName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</results>
</MyRoot>
Output produced
<?xml version="1.0" encoding="utf-16"?>
<MyNewRoot version="0.1">
<author>Employee No 321</author>
<info>Alpha Bravo</info>
<item cost="33" origin="www" level="77">
<GivenName>Jack</GivenName>
<FamilyName>Frost</FamilyName>
<Date>1998-10-30</Date>
<Organization>Lemon growers association</Organization>
</item>
<item cost="65" qry="routine 21" origin="db-11" level="77">
<GivenName>Mike</GivenName>
<FamilyName>Black</FamilyName>
<Date>2006-10-30</Date>
<Organization>Ford Motor Company</Organization>
</item>
</MyNewRoot>
XSL script
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="#default">
<xsl:template match="MyRoot">
<xsl:call-template name="MainTemplate">
</xsl:call-template>
</xsl:template>
<xsl:template name="MainTemplate">
<MyNewRoot version="0.1">
<xsl:copy-of select="results/author" />
<xsl:copy-of select="results/info" />
<xsl:for-each select="results/item">
<xsl:call-template name="FixItemElement"/>
</xsl:for-each>
</MyNewRoot>
</xsl:template>
<xsl:template name="FixItemElement">
<xsl:copy>
<xsl:copy-of select="#*[not(name()='source' or name()='level')]" />
<xsl:attribute name="origin">
<xsl:value-of select="#source"/>
</xsl:attribute>
<xsl:attribute name="level">
<xsl:value-of select="77"/>
</xsl:attribute>
<xsl:for-each select="descendant::*">
<xsl:choose>
<xsl:when test="local-name(.) = 'FirstName'">
<GivenName>
<xsl:value-of select="."/>
</GivenName>
</xsl:when>
<xsl:when test="local-name(.) = 'LastName'">
<FamilyName>
<xsl:value-of select="."/>
</FamilyName>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>

Resources