输入:
<xsl:variable name="nodelist">
<root>
<a size="12" number="11">
<sex>male</sex>
Jens
</a>
<a size="12" number="11">
<sex>male</sex>
Hulk
</a>
<a size="12" number="11">
<sex>male</sex>
Steven XXXXXXXXXXX
</a>
<a size="12" number="11">
<sex>male</sex>
Joschua
</a>
<a size="12" number="11">
<sex>female</sex>
Angelina
</a>
</root>
</variable>
期望的输出:
<root>
<a size="12" number="11">
<sex>male</sex>
Jens
</a>
<a size="12" number="11">
<sex>male</sex>
Hulk
</a>
<a size="12" number="11">
<sex>male</sex>
Steven YYYYYYYYYYYY
</a>
<a size="12" number="11">
<sex>male</sex>
Joschua
</a>
<a size="12" number="11">
<sex>female</sex>
Angelina
</a>
</root>
我想用XXXXXXXXXXX更改节点。 我可以复制第一个和最后两个节点,更改第三个节点,然后再像这样重新组合在一起。 (XLST 1.0)
<xsl:variable name="begin">
<xsl:value-of select="substring-before($nodelist, 'XXXXXXXXXXX')"/>
</xsl:variable>
<xsl:variable name="replaceString">
YYYYYYYYYYYY
</xsl:variable>
<xsl:variable name="end">
<xsl:value-of select="substring-after($nodelist, 'xxxxx')"/>
</xsl:variable>
<xsl:variable name="all">
<xsl:copy-of select="$begin"/>
<xsl:copy-of select="$replaceString"/>
<xsl:copy-of select="$end"/>
</xsl:variable>
使用子字符串我丢失了有关节点的所有信息。这是子字符串
的结果<root>
male Jens
male Hulk
male Steven YYYYYYYYYYYY
male Joschua
female Angelina
</root>
答案 0 :(得分:1)
您需要使样式表更具针对性。仅更改包含需要替换的值的text()
。对于其他所有内容,身份模板将确保复制内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!--Identity template will copy all matched nodes and apply-templates-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!--Specialized template to match on text nodes that contain the "findString" value-->
<xsl:template match="text()[contains(.,'XXXXXXXXXXX')]">
<xsl:variable name="findString" select="'XXXXXXXXXXX'"/>
<xsl:variable name="replaceString" select="'YYYYYYYYYYYY'"/>
<xsl:value-of select="concat(substring-before(., $findString),
$replaceString,
substring-after(., $findString))"/>
</xsl:template>
</xsl:stylesheet>