How to access variable as parameter from another template XSLT - oracle

Hello I have one problem, I have 2 variables(min_value and max_value) they are in different templates MIN and MAX, in MAX I want to calculate the difference between max_value and min_value but the result is NAN since min_value values are not transferred from one template to another.
<xsl:template match="MAX">
<xsl:param name= "min_value"/>
<xsl:variable name="max_value" select= "SHARE_RATE"/>
<span class="max_rate">
MAX: <xsl:apply-templates select="SHARE_RATE"/>
</span>
<br/>
<span class= "diff">
Diff: (<xsl:value-of select="$max_value - $min_value"/>)
</span>
</xsl:template>
<xsl:template match="MIN">
<xsl:variable name="min_value" select="SHARE_RATE"/>
<span class="SHARE_RATE">
MIN: <xsl:apply-templates select="SHARE_RATE"/>
</span>
<xsl:apply-templates select="MAX">
<xsl:with-param name="min_value" select= "$min_value"/>
</xsl:apply-templates>
</xsl:template>
UPDATE
<page shareid="%" min_rate="%" max_rate="%" skip="0">
<TRANSACTIONS>
<MIN num="1">
<SHAREID>0</SHAREID>
<SHARE_RATE>1200</SHARE_RATE>
</MIN>
<MIN num="2">
<SHAREID>1</SHAREID>
<SHARE_RATE>4200</SHARE_RATE>
</MIN>
<MIN num="3">
<SHAREID>2</SHAREID>
<SHARE_RATE>1600</SHARE_RATE>
</MIN>
<MIN num="4">
<SHAREID>3</SHAREID>
<SHARE_RATE>6100</SHARE_RATE>
</MIN>
<MIN num="5">
<SHAREID>4</SHAREID>
<SHARE_RATE>550</SHARE_RATE>
</MIN>
<MIN num="6">
<SHAREID>5</SHAREID>
<SHARE_RATE>420</SHARE_RATE>
</MIN>
<MIN num="7">
<SHAREID>6</SHAREID>
<SHARE_RATE>2000</SHARE_RATE>
</MIN>
</TRANSACTIONS>
<TRANSACTIONS>
<MAX num="1">
<SHAREID>0</SHAREID>
<SHARE_RATE>2100</SHARE_RATE>
</MAX>
<MAX num="2">
<SHAREID>1</SHAREID>
<SHARE_RATE>5200</SHARE_RATE>
</MAX>
<MAX num="3">
<SHAREID>2</SHAREID>
<SHARE_RATE>2000</SHARE_RATE>
</MAX>
<MAX num="4">
<SHAREID>3</SHAREID>
<SHARE_RATE>7000</SHARE_RATE>
</MAX>
<MAX num="5">
<SHAREID>4</SHAREID>
<SHARE_RATE>1000</SHARE_RATE>
</MAX>
<MAX num="6">
<SHAREID>5</SHAREID>
<SHARE_RATE>2520</SHARE_RATE>
</MAX>
<MAX num="7">
<SHAREID>5</SHAREID>
<SHARE_RATE>2520</SHARE_RATE>
</MAX>
</TRANSACTIONS>
</page>
UPDATE 2
<?xml version="1.0" encoding="ISO-8859-2"?>
<?xml-stylesheet type="text/xsl" href="share.xsl"?>
<page connection="labor"
xmlns:xsql="urn:oracle-xsql"
shareid="%"
min_rate= "%"
max_rate= "%"
skip="0">
<xsql:query rowset-element="TRANSACTIONS"
row-element="MIN"
skip-rows="{#skip}"
max-rows="{#max-rows}"
bind-params="shareid min_rate ">
SELECT
f77inq.shares.shareid, A.share_rate
FROM
f77inq.trans A
INNER JOIN f77inq.shares ON A.shareid = f77inq.shares.shareid
WHERE A.shareid LIKE ?
AND A.share_rate=
(
SELECT
MIN(share_rate)
FROM
f77inq.trans B
WHERE
B.shareid = A.shareid
)
AND A.share_rate LIKE?
</xsql:query>
<xsql:query rowset-element="TRANSACTIONS"
row-element="MAX"
skip-rows="{#skip}"
max-rows="{#max-rows}"
bind-params="shareid">
SELECT
f77inq.shares.shareid, A.share_rate
FROM
f77inq.trans A
INNER JOIN f77inq.shares ON A.shareid = f77inq.shares.shareid
WHERE A.shareid LIKE ?
AND A.share_rate=
(
SELECT
MAX(share_rate)
FROM
f77inq.trans B
WHERE
B.shareid = A.shareid
)
</xsql:query>
</page>

I would suggest you use a key to link the MIN and MAX values, based on a common SHAREID. Here's an example that uses XML output for better clarity:
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:strip-space elements="*"/>
<xsl:key name="max" match="MAX" use="SHAREID" />
<xsl:template match="/page">
<root>
<xsl:apply-templates select="TRANSACTIONS/MIN"/>
</root>
</xsl:template>
<xsl:template match="MIN">
<xsl:variable name="id" select="SHAREID" />
<xsl:variable name="min" select="SHARE_RATE" />
<xsl:variable name="max" select="key('max', $id)/SHARE_RATE" />
<share id="{$id}">
<min><xsl:value-of select="$min"/></min>
<max><xsl:value-of select="$max"/></max>
<diff><xsl:value-of select="$max - $min"/></diff>
</share>
</xsl:template>
</xsl:stylesheet>
Applied to your example input, the result is:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<share id="0">
<min>1200</min>
<max>2100</max>
<diff>900</diff>
</share>
<share id="1">
<min>4200</min>
<max>5200</max>
<diff>1000</diff>
</share>
<share id="2">
<min>1600</min>
<max>2000</max>
<diff>400</diff>
</share>
<share id="3">
<min>6100</min>
<max>7000</max>
<diff>900</diff>
</share>
<share id="4">
<min>550</min>
<max>1000</max>
<diff>450</diff>
</share>
<share id="5">
<min>420</min>
<max>2520</max>
<diff>2100</diff>
</share>
<share id="6">
<min>2000</min>
<max>2520</max>
<diff>520</diff>
</share>
</root>

ok, what I understand is, that you want to match MIN/MAX values with the same SHAREID. In this case the following xslt might do what you need:
<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="MAX">
<xsl:variable name="id" select="SHAREID"/>
<!-- or '0' handles the case where no coresponding MIN/SHARE_RATE is found -->
<xsl:variable name="min_value" select="//MIN[SHAREID=$id]/SHARE_RATE or '0'"/>
<xsl:variable name="max_value" select= "SHARE_RATE"/>
<span class="max_rate">
MAX: <xsl:value-of select="$max_value"/>
</span>
<br/>
<span class= "diff">
Diff: (<xsl:value-of select="$max_value - $min_value"/>)
</span>
</xsl:template>
<!-- MAX does it all, so ignore output of MIN -->
<xsl:template match="MIN"/>
<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

Related

XSLT 2.0 Sort by variable from another style sheet

I need to create XML that is sorted by numeric values I pull from another XSLT, which I use as a cross reference. The below source XML (source.xml) has four alpha characters at Partner/Header/#whse.
<?xml version="1.0" encoding="UTF-8"?>
<Partner partnerId="TradingPartner1">
<Header whse="NCCH" >
<Contract claimNumber="00000000" />
</Header>
<Header whse="TXAU" >
<Contract claimNumber="00000000" />
</Header>
<Header whse="LANO" >
<Contract claimNumber="00000000" />
</Header>
<Header whse="MIGR">
<Contract claimNumber="00000000" />
</Header>
<Header whse="TXHO">
<Contract claimNumber="00000000" />
</Header>
</Partner>
I need to cross reference the alpha characters to get the DUNS+4.
I use this XSLT (Duns_config.xslt) to get the DUNS.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="SHIPTODUNS">
<xsl:param name="Whse" />
<xsl:choose>
<xsl:when test="$Whse = 'LANO'"><xsl:value-of select="'0044893600101'" /></xsl:when>
<xsl:when test="$Whse = 'TXHO'"><xsl:value-of select="'0044893600103'" /></xsl:when>
<xsl:when test="$Whse = 'TXAU'"><xsl:value-of select="'0044893600105'" /></xsl:when>
<xsl:when test="$Whse = 'NCCH'"><xsl:value-of select="'0044893600214'" /></xsl:when>
<xsl:when test="$Whse = 'MIGR'"><xsl:value-of select="'8949713340601'" /></xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
In the main XSLT (Transaction.xslt), I include the Duns_config.xslt and call SHIPTODUNS putting the data in the variable $headerDuns. I then get the last three digits of the DUNS+4 and put them into the variable $varWhse and try to sort by this variable:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:include href="Duns_config.xslt"/>
<xsl:template match="Partner">
<Partner partnerId="{./#partnerId}">
<xsl:apply-templates select="./Header" />
</Partner>
</xsl:template>
<xsl:template match="Header">
<xsl:variable name="headerDuns">
<xsl:call-template name = "SHIPTODUNS">
<xsl:with-param name="Whse" select="./#whse" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="varWhse">
<xsl:value-of select="substring($headerDuns, 11, 3)" />
</xsl:variable>
<xsl:for-each select="current()">
<xsl:sort select="$varWhse" />
<transaction varwhse="{$varWhse}">
<duns number="{$headerDuns}" />
</transaction>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The output is not sorted by the $varWhse:
<?xml version="1.0" encoding="UTF-8"?>
<Partner partnerId="TradingPartner1">
<transaction varwhse="214">
<duns number="0044893600214"/>
</transaction>
<transaction varwhse="105">
<duns number="0044893600105"/>
</transaction>
<transaction varwhse="101">
<duns number="0044893600101"/>
</transaction>
<transaction varwhse="601">
<duns number="8949713340601"/>
</transaction>
<transaction varwhse="103">
<duns number="0044893600103"/>
</transaction>
</Partner>
I am wanting the data to come out like this:
<?xml version="1.0" encoding="UTF-8"?>
<Partner partnerId="TradingPartner1">
<transaction varwhse="101">
<duns number="0044893600101"/>
</transaction>
<transaction varwhse="103">
<duns number="0044893600103"/>
</transaction>
<transaction varwhse="105">
<duns number="0044893600105"/>
</transaction>
<transaction varwhse="214">
<duns number="0044893600214"/>
</transaction>
<transaction varwhse="601">
<duns number="8949713340601"/>
</transaction>
</Partner>
Anyone see what I am doing wrong or have another way? This is my first post on this site. It's a lot of information and I hope it makes sense.
One of the problems with your code is that the xsl:sort in your Header template is not the first instruction; xsl:sort has to be the first instruction. Another problem is that the sorting appears in the Header template, but it would have to be applied on an upper level. To correct these mistakes, some restructuring of your templates has to be performed.
For the following solution I chose to put all of the logic into one template - with two steps:
Fill a variable named mapping with a sub-data-structure with item elements.
Iterate over that variable with a sorted xsl:for-each and output the transaction elements.
The result looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:include href="Duns_config.xslt"/>
<xsl:template match="Partner">
<Partner partnerId="{./#partnerId}">
<xsl:variable name="mapping">
<xsl:for-each select="Header">
<xsl:variable name="headerDuns">
<xsl:call-template name = "SHIPTODUNS">
<xsl:with-param name="Whse" select="#whse" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="varWhse">
<xsl:value-of select="substring($headerDuns, 11, 3)" />
</xsl:variable>
<item>
<whse><xsl:value-of select="$varWhse" /></whse>
<duns><xsl:value-of select="$headerDuns" /></duns>
</item>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="$mapping/item">
<xsl:sort select="whse" /> <!-- See how xsl:sort is the first instruction -->
<transaction varwhse="{whse}">
<duns number="{duns}"/>
</transaction>
</xsl:for-each>
</Partner>
</xsl:template>
</xsl:stylesheet>
And its output is:
<?xml version="1.0" encoding="UTF-8"?>
<Partner partnerId="TradingPartner1">
<transaction varwhse="101">
<duns number="0044893600101"/>
</transaction>
<transaction varwhse="103">
<duns number="0044893600103"/>
</transaction>
<transaction varwhse="105">
<duns number="0044893600105"/>
</transaction>
<transaction varwhse="214">
<duns number="0044893600214"/>
</transaction>
<transaction varwhse="601">
<duns number="8949713340601"/>
</transaction>
</Partner>
Which is as desired.
Apply sorting after getting the result of choose. What happen is you are still generating the xml output, but you are sorting it on the Header item level.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:include href="Duns_config.xslt"/>
<xsl:template match="Partner">
<xsl:variable name="partial">
<PartnerTemp partnerId="{./#partnerId}">
<xsl:apply-templates select="./Header" />
</PartnerTemp>
</xsl:variable>
<xsl:apply-templates select="$partial"></xsl:apply-templates>
</xsl:template>
<xsl:template match="PartnerTemp">
<Partner partnerId="{./#partnerId}">
<xsl:perform-sort select="transaction">
<xsl:sort select="#varwhse"/>
</xsl:perform-sort>
</Partner>
</xsl:template>
<xsl:template match="Header">
<xsl:variable name="headerDuns">
<xsl:call-template name = "SHIPTODUNS">
<xsl:with-param name="Whse" select="./#whse" />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="varWhse">
<xsl:value-of select="substring($headerDuns, 11, 3)" />
</xsl:variable>
<xsl:for-each select="current()">
<transaction varwhse="{$varWhse}">
<duns number="{$headerDuns}" />
</transaction>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

XSLT IF evaluation rules

From the XML file :
<store >
<tools>
<tool IDT="T1">
<container>B1</container>
<container>B2</container>
</tool>
<tool IDT="T2">
<container>B1</container>
</tool>
<tool IDT="T3">
<container>B2</container>
</tool>
</tools>
<boxes>
<box IDB="B1" height="10" width="20" length="30" weight="4"/>
<box IDB="B2" height="5" width="40" length="30" weight="2"/>
</boxes>
</store>
I try to display for each box the list of tools that go into each box. For that, I wrote the following XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output
method="html"
encoding="UTF-8"
doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"
indent="yes" />
<xsl:template match="/">
<html>
<head>
<title>Boxes contents</title>
<link type="text/css" rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Boxes contents</h1>
<ul>
<xsl:apply-templates select="/store/boxes/box" />
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="box" >
<li><xsl:text>Box </xsl:text>
<xsl:value-of select="#ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:call-template name="findTools" >
<xsl:with-param name="currentBOX" select="#IDB"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="findTools" >
<xsl:param name="currentBOX" />
<xsl:for-each select="/store/tools/tool/container" >
<xsl:if test="container = $currentBOX" >
<br><xsl:value-of select="#IDT"/></br>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When I do it, I never see the tools. In debug under OXYGEN, I see that the IF is never true. I do not understand why? I start in XPath and XSLT, thanks for your help
You already are at a <container> element inside the <xsl:for-each>. There are no children, so selecting another <container> inside the <xsl:if> won't return anything.
You mean to execute your check from the <tool> node.
<xsl:for-each select="/store/tools/tool">
<xsl:if test="container = $currentBOX">
<xsl:value-of select="#IDT"/><br />
</xsl:if>
</xsl:for-each>
which is easier written as
<xsl:for-each select="/store/tools/tool[container = $currentBOX]">
<xsl:value-of select="#IDT"/><br />
</xsl:for-each>
Overall a more straight-forward way to write the two templates would be this:
<xsl:template match="box">
<li>
<xsl:text>Box </xsl:text>
<xsl:value-of select="#ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:apply-templates select="/store/tools/tool[container = current()/#IDB]" />
</xsl:template>
<xsl:template match="tool">
<xsl:value-of select="#IDT"/><br />
</xsl:template>
And alternatively you can use an <xsl:key> to index <tool> elements by their <container> value:
<xsl:key name="kToolByContainer" match="/store/tools/tool" use="container" />
<xsl:template match="box">
<li>
<xsl:text>Box </xsl:text>
<xsl:value-of select="#ID"/>
<xsl:text>contains the following tools : </xsl:text>
</li>
<xsl:apply-templates select="key('kToolByContainer', #IDB)" />
</xsl:template>
<xsl:template match="tool">
<xsl:value-of select="#IDT"/><br />
</xsl:template>

XSLT for each list value for specified id

I try list value in deep level parameter, but only specified parameter.
I do for each test id for 706 number, when true I do for each and list value name. I need too semicolon between values but not at the beginning and at the end.
XML:
<parameters>
<section id="27" name="Parametry produktu"/>
<parameter id="23" name="kolor">
<value id="42" name="jasny róż"/>
</parameter>
<parameter id="25" name="skład">
<value id="43" name="97% bawełna, 3% poliamid"/>
</parameter>
<parameter id="706" name="rozmiar (ukryć)"">
<value id="717" name="46"/>
<value id="718" name="47"/>
<value id="719" name="48"/>
</parameter>
<parameter id="142" name="płeć (ukryć)">
<value id="746" name="ona"/>
</parameter>
</parameters>
XSLT:
<sizes3>
<xsl:for-each select="parameters">
<xsl:if test="parameter/#id = 706">
<xsl:for-each select="parameter">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="value/#name" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('; ', value/#name)" />
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</sizes3>
Result is:
<sizes3>jasny róż; 97% bawełna, 3% poliamid;46; ona;</sizes3>
But i need:
<sizes3>46;47;48</sizes3>
Here's one way:
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:strip-space elements="*"/>
<xsl:template match="/parameters">
<!-- other stuff ? -->
<sizes3>
<xsl:for-each select="parameter[#id=706]/value">
<xsl:value-of select="#name" />
<xsl:if test="position()!=last()">
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:for-each>
</sizes3>
<!-- more stuff ? -->
</xsl:template>
</xsl:stylesheet>
Simpler, shorter, no <xsl:for-each>, no XSLT conditional operators:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<size3><xsl:apply-templates/></size3>
</xsl:template>
<xsl:template match="parameter[#id=706]/value">
<xsl:apply-templates select="#name"/>
</xsl:template>
<xsl:template match="parameter[#id=706]/value[position() > 1]" priority="2">
<xsl:text>;</xsl:text><xsl:apply-templates select="#name"/>
</xsl:template>
</xsl:stylesheet>

Xpath Query Help. Selecting data from multiple paths as a single data set

I have an xml structure like the following :
<doc>
<line void="false">
<lineNumber>1</lineNumber>
<info1>ddddd</info1>
<info2>aaaaa</info2>
</line>
<line void="true">
<lineNumber>2</lineNumber>
<voidLineNumber>1</voidLineNumber>
<voidValue>2.00</voidValue>
</line>
</doc>
I need one single set of data. I would like to select all the lines where void = false as well as the voidLineNumber and voidValue data from the line where void = true and the voidLineNumber = lineNumber from the original line.
Is this possible? Any help would be appreciated. Thanks
As Michael Kay noted, XPath itself can only be used to select nodes, not to transform them. You can do what you want with XSLT:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" />
<xsl:template match="doc">
<xsl:apply-templates select="line" />
</xsl:template>
<xsl:template match="line">
<xsl:variable name="voidvalue"><xsl:value-of select="#void" /></xsl:variable>
<xsl:if test="$voidvalue='false'">
<xsl:copy>
<xsl:copy-of select="#*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:if test="name(.)='lineNumber'">
<xsl:value-of select="."/>
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Output Context Node (full path) in XSLT 1.0?

For debugging purposes it would be handy to output the full path of the context node from within a template, is there unabbreviated xpath or function to report this ?
Example Template:
<xsl:template match="#first">
<tr>
<td>
<xsl:value-of select="??WHAT TO PUT IN HERE??"/>
</td>
</tr>
</xsl:template>
Example (Abridged) input document:
<people>
<person>
<name first="alan">
...
The output from the template would be something like:
people / person / name / #first
Or something similar.
This transformation produces an XPath expression for the wanted node:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="vNode" select=
"/*/*[2]/*/#first"/>
<xsl:apply-templates select="$vNode" mode="path"/>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat('/',name())"/>
<xsl:variable name="vnumPrecSiblings" select=
"count(preceding-sibling::*[name()=name(current())])"/>
<xsl:variable name="vnumFollSiblings" select=
"count(following-sibling::*[name()=name(current())])"/>
<xsl:if test="$vnumPrecSiblings or $vnumFollSiblings">
<xsl:value-of select=
"concat('[', $vnumPrecSiblings +1, ']')"/>
</xsl:if>
</xsl:template>
<xsl:template match="#*" mode="path">
<xsl:apply-templates select="ancestor::*" mode="path"/>
<xsl:value-of select="concat('/#', name())"/>
</xsl:template>
</xsl:stylesheet>
when applied on the following XML document:
<people>
<person>
<name first="betty" last="jones"/>
</person>
<person>
<name first="alan" last="smith"/>
</person>
</people>
the wanted, correct result is produced:
/people/person[2]/name/#first
Here's a stylesheet (of dubious value) that prints the path to every element and attribute in a document:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:template match="*">
<xsl:param name="pathToHere" select="''" />
<xsl:variable name="precSiblings"
select="count(preceding-sibling::*[name()=name(current())])" />
<xsl:variable name="follSiblings"
select="count(following-sibling::*[name()=name(current())])" />
<xsl:variable name="fullPath"
select="concat($pathToHere, '/', name(),
substring(concat('[', $precSiblings + 1, ']'),
1 div ($follSiblings or $precSiblings)))" />
<xsl:value-of select="concat($fullPath, '
')" />
<xsl:apply-templates select="#*|*">
<xsl:with-param name="pathToHere" select="$fullPath" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="#*">
<xsl:param name="pathToHere" select="''" />
<xsl:value-of select="concat($pathToHere, '/#', name(), '
')" />
</xsl:template>
</xsl:stylesheet>
When applied to this input:
<people>
<person>
<name first="betty" last="jones" />
</person>
<person>
<name first="alan" last="smith" />
</person>
<singleElement />
</people>
Produces:
/people
/people/person[1]
/people/person[1]/name
/people/person[1]/name/#first
/people/person[1]/name/#last
/people/person[2]
/people/person[2]/name
/people/person[2]/name/#first
/people/person[2]/name/#last
/people/singleElement

Resources