Re: parameter concatinating

Subject: Re: parameter concatinating
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 10 Aug 1999 16:39:35 +0200
At 99/08/07 14:51 -0400, Richard Lander wrote:
I am having trouble using parameters, using the July 09 draft. I'd like to use
parameters to pass patterns to named templates, then add to those patterns in
particular situations. For example, I've defined the path parameter for this
purpose. In the following XSLT fragment , I'm trying to add '/@id' to the
pattern in the path parameter.

<xsl:when test="$path/@id">

Variables can only be used in this way when they are of the type "node set", not when they are of any other type. Since you are talking about concatenation, I assumed you are working with string variables.


But it doesn't mean you are stuck.

        <xsl:call-template name="idcatcher_path">
          <xsl:with-param name="catch">url</xsl:with-param>
          <xsl:with-param name="path">child::SECTION[1]</xsl:with-param>
        </xsl:call-template>

Here you are assigning a *result tree fragment* to a variable, and the only things you can do with a result tree fragment is add the fragment structure (using copy-of) or the fragment value (using value-of) to the result tree.


Back to "concatenation", if you wanted to pass a string you would use:

<xsl:with-param name="path" select="'child::SECTION[1]'/>

... but this still won't help you.

But, if you get away from "concatenation" you will find what you are looking for if you use node sets as your variable type:

    <xsl:call-template name="idcatcher_path">
      <xsl:with-param name="catch">url</xsl:with-param>
      <xsl:with-param name="path" select="child::SECTION[1]"/>
    </xsl:call-template>

This will assign the node set relative to the current position to the variable named "path" and *then* you can do test="$path/@id".

An example is below, using your named template verbatim.

I hope this helps.

....... Ken


T:\FTEMP>type test.xml <?xml version="1.0"?> <MAIN> <SECTION id="instance-id"/> <SECTION/> </MAIN> T:\FTEMP>type test.xsl <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0";>

<xsl:template match="MAIN">
  <first>
    <xsl:call-template name="idcatcher_path">
      <xsl:with-param name="catch">url</xsl:with-param>
      <xsl:with-param name="path" select="child::SECTION[1]"/>
    </xsl:call-template>
  </first>
  <second>
    <xsl:call-template name="idcatcher_path">
      <xsl:with-param name="catch">url</xsl:with-param>
      <xsl:with-param name="path" select="child::SECTION[2]"/>
    </xsl:call-template>
  </second>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template name="idcatcher_path">
  <xsl:param name="catch"/>
  <xsl:param name="path"/>
  <xsl:choose>
    <xsl:when test="$path/@id">
      <xsl:attribute name="{$catch}"><xsl:value-of
                     select="$path/@id"/></xsl:attribute>
    </xsl:when>
    <xsl:otherwise>
      <xsl:attribute name="{$catch}"><xsl:value-of
                     select="generate-id($path)"/></xsl:attribute>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

T:\FTEMP>call xsl test.xml test.xsl testout.xml
T:\FTEMP>type testout.xml
<first url="instance-id"/><second url="N6"/>

T:\FTEMP>

--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-01-2
Next instructor-led training:   MS'99 1999-08-16  MT'99 1999-12-05/06


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread