【问题标题】:XSLT在检查属性是否相同后用另一个xml替换1个xml中标签的文本值然后替换文本值
【发布时间】:2022-01-21 23:10:37
【问题描述】:

我是这方面的初学者,我想使用 xslt 来转换我的 xml 文件,但我缺乏这样做的知识和经验。这是我想用 xsl 转换的 file1.xml 的示例

file1.xml

<shop>
    <SHOPITEM>
        <CATEGORY id="12306">ABCclothes</CATEGORY>
    </SHOPITEM>
    <SHOPITEM>
        <CATEGORY id="1233">SDFclothes</CATEGORY>
    </SHOPITEM>
    <SHOPITEM>
        <CATEGORY id="12308">CDFclothes</CATEGORY>
    </SHOPITEM>
</shop>

在检查 CATEGORY 中的属性 id 是否与 file2.xml 中的 CATEGORY2 中的 id 相同后, 然后将来自 file1.xml 的元素文本 CATEGORY 替换为来自 file2.xml 的 CATEGORY2 的元素文本

file2.xml

<ITEM>
      <CATEGORY2 id="12308">CDFreplacetext<CATEGORY2>
      <CATEGORY2 id="12306">ABCreplacetext<CATEGORY2>
</ITEM>

这是我想要得到的输出

输出:

<shop>
    <SHOPITEM>
        <CATEGORY id="12306">ABCreplacetext</CATEGORY> 
    </SHOPITEM>
    <SHOPITEM>
        <CATEGORY id="1233">SDFclothes</CATEGORY>
    </SHOPITEM>
    <SHOPITEM>
        <CATEGORY id="12308">CDFreplacetext</CATEGORY>
    </SHOPITEM>
</shop>

【问题讨论】:

    标签: xml xslt replace


    【解决方案1】:

    如果您可以使用 XSLT 3.0 版(或 2.0;只需将 xsl:mode 替换为 identity transform)我会使用 xsl:key...

    <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
        
        <xsl:variable name="file2" select="document('file2.xml')"/>
        
        <xsl:key name="cat2" match="CATEGORY2" use="@id"/>
        
        <xsl:mode on-no-match="shallow-copy"/>
        
        <xsl:template match="CATEGORY">
            <xsl:copy>
                <xsl:apply-templates select="@*,(key('cat2',@id,$file2)/node(),node())[1]"/>
            </xsl:copy>
        </xsl:template>
        
    </xsl:stylesheet>
    

    如果您无法使用 XSLT 1.0,请尝试使用 xsl:choose...

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
        
        <xsl:variable name="file2" select="document('file2.xml')"/>
            
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
        
        <xsl:template match="CATEGORY">
            <xsl:copy>
                <xsl:apply-templates select="@*"/>
                <xsl:choose>
                    <xsl:when test="$file2//CATEGORY2[@id=current()/@id]">
                        <xsl:apply-templates select="$file2//CATEGORY2[@id=current()/@id]/node()"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="node()"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:copy>
        </xsl:template>
        
    </xsl:stylesheet>
    

    【讨论】:

    • 非常感谢,先生。这就是我要找的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    相关资源
    最近更新 更多