Calling the same xsl:template for different node names of the same complex type

Posted by CraftyFella on Stack Overflow See other posts from Stack Overflow or by CraftyFella
Published on 2010-03-24T16:37:57Z Indexed on 2010/03/24 17:13 UTC
Read the original article Hit count: 537

Filed under:
|
|
|

Hi,

I'm trying to keep my xsl DRY and as a result I wanted to call the same template for 2 sections of an XML document which happen to be the same complex type (ContactDetails and AltContactDetails). Given the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<RootNode>
    <Name>Bob</Name>
    <ContactDetails>
        <Address>
            <Line1>1 High Street</Line1>
            <Town>TownName</Town>
            <Postcode>AB1 1CD</Postcode>
        </Address>
        <Email>[email protected]</Email>
    </ContactDetails>
    <AltContactDetails>
        <Address>
            <Line1>3 Market Square</Line1>
            <Town>TownName</Town>
            <Postcode>EF2 2GH</Postcode>
        </Address>
        <Email>[email protected]</Email>
    </AltContactDetails>
</RootNode>

I wrote an XSL Stylesheet as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <PersonsName>
            <xsl:value-of select="RootNode/Name"/>
        </PersonsName>
        <xsl:call-template name="ContactDetails">
            <xsl:with-param name="data"><xsl:value-of select="RootNode/ContactDetails"/></xsl:with-param>
            <xsl:with-param name="elementName"><xsl:value-of select="'FirstAddress'"/></xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="ContactDetails">
            <xsl:with-param name="data"><xsl:value-of select="RootNode/AltContactDetails"/></xsl:with-param>
            <xsl:with-param name="elementName"><xsl:value-of select="'SecondAddress'"/></xsl:with-param>
        </xsl:call-template>
    </xsl:template>
    <xsl:template name="ContactDetails">
        <xsl:param name="data"></xsl:param>
        <xsl:param name="elementName"></xsl:param>
        <xsl:element name="{$elementName}">
            <FirstLine>
                <xsl:value-of select="$data/Address/Line1"/>
            </FirstLine>

            <Town>
                <xsl:value-of select="$data/Address/Town"/>
            </Town>
            <PostalCode>
                <xsl:value-of select="$data/Address/Postcode"/>
            </PostalCode>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

When i try to run the style sheet it's complaining to me that I need to:

To use a result tree fragment in a path expression, either use exsl:node-set() or specify version 1.1

I don't want to go to version 1.1.. So does anyone know how to get the exsl:node-set() working for the above example?

Or if someone knows of a better way to apply the same template to 2 different sections then that would also really help me out?

Thanks

Dave

© Stack Overflow or respective owner

Related posts about xslt

Related posts about xsl