开发者

How to write a predicate to match the dollar sign character?

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 str开发者_开发技巧ing $A$)?


Try this:

propery[@name='value' and ../property[@name='name' and string = '$A$']]


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.


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$']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜