fatpanther
fatpanther

Reputation: 115

XSLT 2.0 - passing a node set as a parameter doesn't seem to work

I have a document that I need to transform such that most elements are copied as-is, with some exceptions: for certain specified nodes, child elements need to be appended, and some of these child elements need to reference back to specific elements in the source document. A separate "model/crosswalk" xml file contains the elements to add. The elements in the crosswalk that need to refer back to the source document have "source" attributes that point them to specific elements. Of course, my actual source docs (& the actual crosswalk) are more complex and varied than these examples.

Here is a source document example:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <album>
        <artist>Frank Sinatra</artist>
        <title>Greatest Hits</title>
    </album>
    <album>
        <artist>Miles Davis</artist>
        <title>Kind Of Blue</title>
    </album>
    <movie>
        <title>ET</title>
        <director>Steven Spielberg</director>
    </movie>
    <movie>
        <title>Blues Brothers</title>
        <director>John Landis</director>
    </movie>
</root>

Here is the "crosswalk" (crswlk.xml):

<?xml version="1.0" encoding="UTF-8"?>

<root>
    <album>
        <artist-info>
            <artist2 source="artist"/>
        </artist-info>
    </album>
    <movie>
        <director-info>
            <director2 source="director"/>
        </director-info>
    </movie>
</root>

And here is the desired output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <album>
        <artist>Frank Sinatra</artist>
        <title>Greatest Hits</title>
        <artist-info>
            <artist2>Frank Sinatra</artist2>
        </artist-info>
    </album>
    <album>
        <artist>Miles Davis</artist>
        <title>Kind Of Blue</title>
        <artist-info>
            <artist2>Miles Davis</artist2>
        </artist-info>
    </album>
    <movie>
        <title>ET</title>
        <director>Steven Spielberg</director>
        <director-info>
            <director2>Steven Spielberg</director2>
        </director-info>
    </movie>
    <movie>
        <title>Blues Brothers</title>
        <director>John Landis</director>
        <director-info>
            <director2>John Landis</director2>
        </director-info>
    </movie>
</root>

Here's my xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="crosswalk" select="document('crswlk.xml')"/>

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

    <xsl:template match="*/album|movie">
        <xsl:variable name="theNode" select="."/>
        <xsl:variable name="nodeName" select="name()"/>
        <xsl:element name="{$nodeName}">
            <xsl:apply-templates select="@* | node()"/>
            <xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
                <xsl:with-param name="curNode" select="$theNode"/>
            </xsl:apply-templates>
        </xsl:element>
    </xsl:template>

    <xsl:template match="*[@source]">
        <xsl:param name="curNode" />
        <xsl:variable name="sourceNodeName" select="@source"/>
        <xsl:element name="{name()}">
            <xsl:value-of select="$curNode//*[name()=$sourceNodeName]"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

This stops processing once that last template tries to access $curNode for the first time. When I run the xslt in Eclipse (Xalan 2.7.1) it throws this error: " java.lang.ClassCastException: org.apache.xpath.objects.XString cannot be cast to org.apache.xpath.objects.XNodeSet".

If I pass a similar nodeset as a parameter to a template that matches nodes from the source document, it works as expected - the nodeset is accessible. However, passing the nodeset to the last template above doesn't work. Is it because the template matches nodes from the external document? I sure don't know. Any help much appreciated, it took me a while just to get to this point. Thanks!

Upvotes: 2

Views: 6571

Answers (2)

Tim C
Tim C

Reputation: 70598

If you are using XSLT 2.0, then Daniel Haley's answer with tunnelling is surely the way to go. If, however, you are actually using Xalan, and therefore only XSLT 1.0, you need to take a different approach.

The problems start on this line:

<xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">

This will select either artist-info or director-info in your cross walk document, but you have no specific template matching these, so the generic identity template you are using will match them

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

But this does not take any parameter, not does it pass any parameters on. Therefore, when your last template <xsl:template match="*[@source]"> is matched, the curNode will be empty (an empty string), which is not a node-set, and that saddens Xalan.

So, to solve this in XSLT1.0, just add the parameter to the identity template, and pass it on:

<xsl:template match="node()|@*">
    <xsl:param name="curNode" />
    <xsl:copy>
        <xsl:apply-templates select="@* | node()">
            <xsl:with-param name="curNode" select="$curNode" />
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

If the template is ever matched when there is no parameter being passed, it will just pass on an empty parameter without any issue.

Here is the full XSLT (also with the correction of apostrophes being removed from the xsl:value-of, as mentioned in Daniel's answer).

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="crosswalk" select="document('crswlk.xml')"/>

    <xsl:template match="node()|@*">
        <xsl:param name="curNode" />
        <xsl:copy>
            <xsl:apply-templates select="@* | node()">
                <xsl:with-param name="curNode" select="$curNode" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*/album|movie">
        <xsl:variable name="theNode" select="."/>
        <xsl:variable name="nodeName" select="name()"/>
        <xsl:element name="{$nodeName}">
            <xsl:apply-templates select="@* | node()"/>
            <xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
                <xsl:with-param name="curNode" select="$theNode"/>
            </xsl:apply-templates>
        </xsl:element>
    </xsl:template>

    <xsl:template match="*[@source]">
        <xsl:param name="curNode" />
        <xsl:variable name="sourceNodeName" select="@source"/>
        <xsl:element name="{name()}">
            <xsl:value-of select="$curNode//*[name()=$sourceNodeName]"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

When applied to your XML documents, the following is output

<root>
   <album>
      <artist>Frank Sinatra</artist>
      <title>Greatest Hits</title>
      <artist-info>
         <artist2>Frank Sinatra</artist2>
      </artist-info>
   </album>
   <album>
      <artist>Miles Davis</artist>
      <title>Kind Of Blue</title>
      <artist-info>
         <artist2>Miles Davis</artist2>
      </artist-info>
   </album>
   <movie>
      <title>ET</title>
      <director>Steven Spielberg</director>
      <director-info>
         <director2>Steven Spielberg</director2>
      </director-info>
   </movie>
   <movie>
      <title>Blues Brothers</title>
      <director>John Landis</director>
      <director-info>
         <director2>John Landis</director2>
      </director-info>
   </movie>
</root>

Upvotes: 3

Daniel Haley
Daniel Haley

Reputation: 52848

Looks like you need to change 2 things:

  • add tunnel="yes" to xsl:with-param and xsl:param
  • remove the apostrophes from '$sourceNodeName' in the predicate of the xsl:value-of

Updated XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="crosswalk" select="document('crswlk.xml')"/>

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

    <xsl:template match="*/album|movie">
        <xsl:variable name="theNode" select="."/>
        <xsl:variable name="nodeName" select="name()"/>
        <xsl:element name="{$nodeName}">
            <xsl:apply-templates select="@* | node()"/>
            <xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
                <xsl:with-param name="curNode" select="$theNode" tunnel="yes"/>
            </xsl:apply-templates>
        </xsl:element>
    </xsl:template>

    <xsl:template match="*[@source]">
        <xsl:param name="curNode" tunnel="yes"/>
        <xsl:variable name="sourceNodeName" select="@source"/>
        <xsl:element name="{name()}">
            <xsl:value-of select="$curNode//*[name()=$sourceNodeName]"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

Also, you can remove a few of those extra xsl:variables...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:variable name="crosswalk" select="document('crswlk.xml')"/>

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

    <xsl:template match="album|movie">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
            <xsl:apply-templates select="$crosswalk/*/*[name()=current()/name()]/*">
                <xsl:with-param name="curNode" select="." tunnel="yes"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[@source]">
        <xsl:param name="curNode" tunnel="yes"/>
        <xsl:copy>
            <xsl:value-of select="$curNode//*[name()=current()/@source]"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 4

Related Questions