XSLT Template: Reusing with document()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dutone

    XSLT Template: Reusing with document()

    Is it possible to use a template and then, in the same transformation,
    reuse it for a call to document()?

    I have a document that has a child. If necessary, I need to include
    another document's (same structure) child. I'd like to reuse a
    template but it doesn't seem to be working out for me.

    <xsl:template match="/root">
    <id><xsl:valu e-of select="name"/></id>
    <children>
    <xsl:apply-templates select="child"/>
    <xsl:if test="string-length($additon al-id)>0">
    <xsl:apply-templates select="documen t($additional-id)/
    root"/>
    </xsl:if>
    </children>
    </xsl:template>

    <xsl:template match="child">
    ....
    </xsl:template>

    The code above will cause an infinite loop. I've tried passing the /
    root/child element as a param to the child template, but that causes a
    type mismatch error.

    Any ideas? I'd hope that I wouldn't have to recreate the xsl:value-of
    the elements of the child structure with a document()/root/.
  • Richard Tobin

    #2
    Re: XSLT Template: Reusing with document()

    In article <d7778cf0-42f4-4d23-9fda-29455375b1fa@b5 g2000pri.google groups.com>,
    dutone <dutone@hotmail .comwrote:
    >I have a document that has a child. If necessary, I need to include
    >another document's (same structure) child. I'd like to reuse a
    >template but it doesn't seem to be working out for me.
    >
    ><xsl:templat e match="/root">
    <id><xsl:valu e-of select="name"/></id>
    <children>
    <xsl:apply-templates select="child"/>
    <xsl:if test="string-length($additon al-id)>0">
    <xsl:apply-templates select="documen t($additional-id)/
    >root"/>
    </xsl:if>
    </children>
    ></xsl:template>
    >
    ><xsl:templat e match="child">
    ....
    ></xsl:template>
    >
    >The code above will cause an infinite loop.
    You have a template that calls itself, and the recursive call
    will be applied to the same element, so yes it will recurse indefinitely.

    You could pass a parameter to the template that is the name of
    the document to recurse on. Have a template for / that passes it
    the second document's name, and have the recursive call pass in an
    empty string (which you already check for).

    -- Richard
    --
    :wq

    Comment

    • Joseph J. Kesselman

      #3
      Re: XSLT Template: Reusing with document()

      Unless $additional-id changes each time around, the code you've shown
      will indeed recurse endlessly, re-reading the same document(). There are
      many possible solutions; which one makes sense depends on the details of
      your problem.

      For example: compute the variable from the source document so it's
      different in each invocation. Or make it a template parameter and have
      this recursion pass a different value down. Or use modes to distinguish
      between processing the main input document and the one read via
      document. Or ensure that the imported document doesn't have a /root
      element and thus won't match this template and recurse again. Or...


      Comment

      Working...