XSLT Template: Reusing with document()

Discussion in 'XML' started by dutone, Apr 25, 2008.

  1. dutone

    dutone Guest

    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:value-of select="name"/></id>
    <children>
    <xsl:apply-templates select="child"/>
    <xsl:if test="string-length($additonal-id)>0">
    <xsl:apply-templates select="document($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/.
    dutone, Apr 25, 2008
    #1
    1. Advertising

  2. In article <>,
    dutone <> wrote:

    >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:value-of select="name"/></id>
    > <children>
    > <xsl:apply-templates select="child"/>
    > <xsl:if test="string-length($additonal-id)>0">
    > <xsl:apply-templates select="document($additional-id)/
    >root"/>
    > </xsl:if>
    > </children>
    ></xsl:template>
    >
    ><xsl:template 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
    Richard Tobin, Apr 25, 2008
    #2
    1. Advertising

  3. 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...
    Joseph J. Kesselman, Apr 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vince C.
    Replies:
    1
    Views:
    2,284
    Vince C.
    Jul 2, 2003
  2. Bernd Fuhrmann
    Replies:
    0
    Views:
    440
    Bernd Fuhrmann
    Feb 12, 2004
  3. Replies:
    4
    Views:
    672
  4. Replies:
    0
    Views:
    303
  5. ubi_comp
    Replies:
    0
    Views:
    135
    ubi_comp
    Nov 29, 2005
Loading...

Share This Page