Reputation: 651
Given the following document:
<foo>
<object>
<property name="value"> <!-- MATCH THIS NODE -->
<string>alpha</string>
</property>
<property name="name">
<string>$A$</string>
</property>
</object>
<object>
<property name="value">
<string>bravo</string>
</property>
<property name="name">
<string>$B$</string>
</property>
</object>
</foo>
and a stylesheet based on the identity transform:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- HAVING PROBLEMS HERE -->
<xsl:template match="property[@name='value'][../property[@name='name']/string='$A$']">
Replace with text!
</xsl:template>
</xsl:stylesheet>
What predicates would I use if I want to match node indicated on the original document when I need to key off the contents of the sibling property/string element (the string $A$)?
Upvotes: 2
Views: 736
Reputation: 338278
For the sake of completeness, the "top-down" XPath:
/foo/object[property[@name='name']='$A$']/property[@name='value']
Though an XML structure like the following would make much more sense:
<foo>
<object>
<property name="$A$">
<string>alpha</string>
</property>
</object>
<object>
<property name="$B$">
<string>bravo</string>
</property>
</object>
</foo>
because you could do
/foo/object/property[@name='$A$']
Upvotes: 0
Reputation: 57956
Try this:
propery[@name='value' and ../property[@name='name' and string = '$A$']]
Upvotes: 1
Reputation: 2416
I'm not sure if this is generic enough for your needs, but this should get you close:
property[following-sibling::property[1]/string = '$A$']
This matches the property node which where the next sibling property has a child string where the text equals "$A$"
This should work if you have the same template with two property nodes, but would need to be adjusted if your XML has more property nodes.
Upvotes: 1