Menu

temporary tree - apply template - parameters

Help
Anonymous
2002-02-27
2012-10-08
  • Anonymous

    Anonymous - 2002-02-27

    I'm currently working on a project that shall generate java code fragements from an XML defintion.

    I now have a problem with xslt and I wrote a small xml and xsl file that shall demonstrate it.

    Basically the goal is to have a "form with blanks", defined in the XSL document.
    The blanks are then filled in with data retrieved from the XML document.

    XML file -----------------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="Y:\4_Transformation\proposal_03_2a\try11min.xsl"?>
    <firstNames>
        <firstName>Andre</firstName>
        <firstName>Rene</firstName>
        <firstName>Marcel</firstName>
    </firstNames>

    /XML file ---------------------------------------

    XSL file ----------------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="firstNames">
        <xsl:apply-templates select="firstName" mode="meta-data-add"/>
    </xsl:template>

    <xsl:template match="firstName" mode="meta-data-add">
        <xsl:variable name="code-with-blanks">
            _<insertFirstName/>_Bellorini_   
        </xsl:variable>
        <xsl:apply-templates select="$code-with-blanks" mode="fill-in-blanks">
            <xsl:with-param name="test" select="5"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="insertFirstName" mode="fill-in-blanks">
        <xsl:param name="test"/>
        <xsl:text>called</xsl:text>_<xsl:value-of select="$test"/>
    </xsl:template>

    </xsl:stylesheet>

    /XSL file ---------------------------------------

    Expected Output:

    _called_5_Bellorini_   
    _called_5_Bellorini_   
    _called_5_Bellorini_

    Output from Instant-Saxon 6.0.2 and

    _called__Bellorini_   
    _called__Bellorini_   
    _called__Bellorini_   

    The parameter does not work in this case!!

    Error Message fron MSXML3 & MSXML4:

    Expression must evaluate to a node-set
    --> $code-with-blanks <--

    My questions:

    Is my approach illegal?
    Are there alternatives?
    Is it just a syntax problem?

    Thank you very much!

     
    • Michael Kay

      Michael Kay - 2002-03-02

      A classic problem. Your apply-templates is selecting the root node of the temporary tree. There is no explicit template rule for this node, so the built-in template is executed. This in turn applies templates to the children of the root - the <insertFirstName> element - which invokes your explicit template rule. But the built-in template rule does not pass parameters through to the explicit template rule. the solution is to write xsl:apply-templates select="$code-with-blanks/*".

      Mike Kay

       
    • Anonymous

      Anonymous - 2002-03-02

      Thank you very much! It really solved my problems.

      Although MSXML does still report an error:

      "Expression must evaluate to a node-set
      $code-with-blanks/*"

      Infoteria iXSLT 2.0c does not cope with it as well.

      I do not really care about that since I'm planning to stick with Saxon.

      Nevertheless could you confirm that my approach complies with the XSLT standard?

      Thank you very much

       
      • Michael Kay

        Michael Kay - 2002-03-03

        XSLT 1.0 doesn't allow you to apply-templates to a temporary tree (or "result tree fragment"). This restriction is relaxed in the XSLT 1.1 and XSLT 2.0 working drafts (and in Saxon). Most XSLT 1.0 processors including MSXML3 provide a way of doing it using an xx:node-set() extension function:

        <xsl:apply-templates select="xx:node-set($code-with-blanks/*)"/>

        Mike Kay