我刚接触XSL,正在寻找一种方法来替换XML中的文本。我的源XML是:
<A>
<key>One</key>
<string>value1</string>
<key>Two</key>
<string>value2</string>
<key>Three</key>
<string>value3</string>
</A>我想要的是只替换其中一个元素。结果应该是:
<A>
<key>One</key>
<string>value1</string>
<key>Two</key>
<string>xxx</string> <---- change this (for key Two)
<key>Three</key>
<string>value3</string>
</A>如何创建一个xsl样式表来管理它?
提前感谢!
发布于 2012-12-06 22:34:23
这似乎起到了关键作用:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="string">
<xsl:choose>
<xsl:when test="preceding-sibling::key[position() = 1 and text() = 'Two']">
<string>replacement</string>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>关键片段是preceding-sibling轴的使用。All available axes are documented here in the xpath specification。
https://stackoverflow.com/questions/13745515
复制相似问题