3

I have 2 files

catalog.xml

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <CD Title="Still got the blues" vinyl="unknown"/>
    <CD Title="When a man loves a woman" vinyl="unknown"/>
</catalog>

vinyl.xml

<?xml version="1.0" encoding="UTF-8"?>
<Vinyl>
    <Album>
        <Title>When a man loves a woman</Title>
        <Vinyl>Yes</Vinyl>
    </Album>
</Vinyl>

How to produce such output.xml with xslt?

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <CD Title="Still got the blues" vinyl="unknown"/>
    <CD Title="When a man loves a woman" vinyl="yes"/>***   
</catalog>

***mark this line becouse it changed in output.xml

1 Answer 1

4

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>
4
  • 1
    Now simplified by matching the attribute directly. Aug 9, 2011 at 13:56
  • 1
    @Kirill thanks. That's the most immediate answer to me. Would like to see an alternative not using control instructions like choose. Aug 9, 2011 at 13:58
  • 1
    @empo really thanks I was making with nested loop on vinyl.xml and catalog.xml. And get upset, becouse no success
    – qwazer
    Aug 9, 2011 at 14:07
  • 1
    @popalka you are welcome. Thanks for the feedback. See edited answer for full XPath usage. Aug 9, 2011 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.