XSLT Template: Reusing with document()

dutone

Usenet Archive
Joined
Jul 1, 2004
Messages
12
Reaction score
0
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/.
 

Richard Tobin

Usenet Archive
Joined
Jan 6, 2000
Messages
4,251
Reaction score
0
dutone said:
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
 

Joseph J. Kesselman

Usenet Archive
Joined
Apr 1, 2008
Messages
98
Reaction score
0
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,244
Members
248,133
Latest member
DujonT

Latest Threads

Top