【问题标题】:Match and change tag value after xsl copy-of select node()选择节点()的xsl复制后匹配和更改标签值
【发布时间】:2018-02-06 08:36:54
【问题描述】:

我收到不同的 xml。其中一个具有值为 true/false 的标签,我必须将其替换为 1/0。 我的 xsl:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <tag1>
        <tag2>
            <xsl:choose>
                <xsl:when test="string-length(ERRORMSG) > 0">
                    <xsl:apply-templates select="ERRORMSG" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:template match="node()|@*">
                        <xsl:copy>
                            <xsl:apply-templates select="node()|@*"/>
                        </xsl:copy>
                    </xsl:template>
                </xsl:otherwise>
            </xsl:choose>
        </tag2>
    </tag1>
</xsl:template>

我的 xml:

<tag2>
    <tag3>
        <tag4>
            <tag5>abc</tag5>
            <tag6>
                <tag7>
                    <tag8>false</tag8>
                    <tag9>true</tag9>
                </tag7>
            </tag6>
        </tag4>
    </tag3>
</tag2>

我该怎么做?我无法删除&lt;xsl:apply-templates select="node()|@*"/&gt;,因为我收到了不同的模板,并且在其中一个模板中,如果真/假,我必须将特定标签值(tag8,tag9)替换为 1/0。

【问题讨论】:

  • 使用两个模板:一个identity transform模板作为规则复制所有节点,一个模板匹配真/假元素作为规则的例外。 -- 注意模板不能嵌套。

标签: xml xslt


【解决方案1】:

你也可以试试这个:

XSLT 1.0

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <!-- Identity Transform -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[normalize-space(.) = 'true' or normalize-space(.) = 'false']">
        <xsl:copy>
            <xsl:choose>
                <xsl:when test="normalize-space(.) = 'true'">1</xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XSLT 2.0

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <!-- Identity Transform -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[normalize-space(.) = 'true' or normalize-space(.) = 'false']">
        <xsl:copy>
            <xsl:value-of select="if(normalize-space(.) = 'true') then '1' else '0'"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

输出

 <tag2>
    <tag3>
        <tag4>
            <tag5>abc</tag5>
            <tag6>
                <tag7>
                    <tag8>0</tag8>
                    <tag9>1</tag9>
                </tag7>
            </tag6>
        </tag4>
    </tag3>
</tag2>

【讨论】:

    【解决方案2】:

    这可以通过修改 XSLT 来实现,首先使用 identity transform 复制所有节点,然后使用模板修改 &lt;tag8&gt;&lt;tag9&gt; 节点中的值。

    XSLT

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:variable name="true" select="'1'" />
        <xsl:variable name="false" select="'0'" />
    
        <!-- Identity Transform -->
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="tag8">
            <xsl:copy>
                <xsl:value-of select="$false" />
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="tag9">
            <xsl:copy>
                <xsl:value-of select="$true" />
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    输出

    <tag2>
        <tag3>
            <tag4>
                <tag5>abc</tag5>
                <tag6>
                    <tag7>
                        <tag8>0</tag8>
                        <tag9>1</tag9>
                    </tag7>
                </tag6>
            </tag4>
        </tag3>
    </tag2>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多