Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT Template: Reusing with document()

Reply
Thread Tools

XSLT Template: Reusing with document()

 
 
dutone
Guest
Posts: n/a
 
      04-25-2008
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/.
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      04-25-2008
In article <(E-Mail Removed)>,
dutone <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Joseph J. Kesselman
Guest
Posts: n/a
 
      04-25-2008
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...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reusing HttpWebRequest.GetRequestStream Dan ASP .Net 4 06-01-2007 07:29 AM
Reusing DataReader Thomas Scheiderich ASP .Net 11 04-13-2004 08:20 AM
Reusing DataSet/DataTable anon ASP .Net 1 04-01-2004 04:53 PM
reusing a bit of code? Rob Meade ASP .Net 4 01-30-2004 02:47 PM
Dynamically reusing XSLT templates across XSL files Vince C. XML 1 07-02-2003 07:55 PM



Advertisments