XSLT for replace attribute in XML element if it exists in another XML?

后端 未结 1 1613
轻奢々
轻奢々 2021-01-14 18:30

I have 2 files

catalog.xml



    

        
相关标签:
1条回答
  • 2021-01-14 19:28

    The following transform, uses catalog.xml as input and loads vinyl.xml using document(). It performs the merge just by making a simple test.


    [XSLT 1.0]

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:variable name="vinyl" select="document('test_i2.xml')"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="@vinyl">
            <xsl:attribute name="vinyl">
                <xsl:variable name="test" select="
                     $vinyl/Vinyl/Album[Title=current()/../@Title]/Vinyl"/>
                <xsl:choose>
                    <xsl:when test="$test">
                        <xsl:value-of select="$test"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
        </xsl:template>
    
    </xsl:stylesheet>
    

    This template for the attribute is less immediate, but it exploits pure XPath:

    <xsl:template match="@vinyl">
        <xsl:attribute name="vinyl">
            <xsl:value-of select="
              $vinyl/Vinyl/Album[Title=current()/../@Title]/Vinyl
              |
              self::node()[count($vinyl/Vinyl/Album[Title=current()/../@Title])=0]"/>
        </xsl:attribute>
    </xsl:template>
    
    0 讨论(0)
提交回复
热议问题