1

输入:

<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 更改 a 节点。我可以复制第一个和最后两个节点,更改第三个,然后像这样重新组合在一起吗?(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>
4

1 回答 1

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>
于 2013-04-21T03:02:39.153 回答