Reputation: 71
I'm using a Java application based on Saxon for the XPath parser. Please consider the following:
<import>
<record ref="abc">
<id>123</id>
<data>Value<data>
</record>
<record ref="def">
<parent>123</parent>
<data>Value2</data>
</record>
</import>
My use case is, I'm looping through the record nodes and need to look up another with attribute ref equal to the value under the child parent node.
If the current node is <record ref="def">
, how can I write a query to return the data node within the first record node by matching on /query/record/id/text() = current node/parent/text()
?
If I execute this query:
/import/record[id/text() = '123']/data
Then I get the correct data node, but I can't seem able to replace '123' for the value under the parent node?
I've tried replacing '123' with ./parent/text()
and while the XPath compiles, no results are returned.
Thanks,
JB
Upvotes: 2
Views: 1902
Reputation: 71
For some reason, current() exists in XSLT but not for XPath against a DOM. You can hard code something like this:
/import/record[id = /import/record[position()]/parent]/data
Upvotes: 0
Reputation: 23383
With the period you are referencing a node in the path context, you should be able to use variables to fix your problem:
<xsl:template match="/import/record[@parent]">
<xsl:variable name="pid" select="./parent/text()" />
<xsl:for-each select="/import/record[id/text() = $pid">
<!-- actions -->
</xsl:for-each>
</xsl:template>
edit
You might get away with something like the following single xpath expression:
/import/record[id/text() = /import/record/parent/text()]/data
(select those records that are referenced by others)
Upvotes: 1
Reputation: 15189
The period is used to refer to the current node. However you have a flaw in your logic: What your path
/import/record[id/text() = ./parent/text()]/data
defines is a <record>
that has a parent
and a id
child which must have the same content.
Upvotes: 0