Re: [xsl] Selecting and printing certain nodes

Subject: Re: [xsl] Selecting and printing certain nodes
From: "Glenn MacGregor" <gtm@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 19 Feb 2004 17:41:09 -0500

Thanks for the response! The following messages may shead some light on why
I am doing it this way.

Although my input no longer looks like:

<tr><td><var/> Test</td><td><var/> Test</td></tr>

It looks like:

<foreach param="devices">
<tr><td><b>BOLDFACE</b> Some Text</td><td>Here</td></tr>
<tr><td> Test11</td><td>Test</td></tr>

Along with this fragment of XML I also have another XML file name @param.xml
(devices.xml) which contains an XML list of all the devices I want to loop

so here is the foreach template:
  <xsl:template match="foreach">
  <xsl:variable name="foreachNode" select="current()" />
  <xsl:variable name="nodeLocation" select="concat($tmpdir,@param,'.xml')"
     <xsl:for-each select="document($nodeLocation)/doc/item">
        <xsl:call-template name="foreachOutput">
         <xsl:with-param name="cdata" select="text()" />
         <xsl:with-param name="do-sect" select="$foreachNode/*" />

Opens the devices.xml file and gets each item calling foerachOutput with
$cdata = dev1 ... and do-sect = all the children of the current node.

Here is the foreachOutput template:
  <xsl:template name="foreachOutput">
    <xsl:param name="cdata" />
    <xsl:param name="do-sect" />
    <xsl:for-each select="$do-sect">
            <xsl:when test="name() = 'replace'">
                <xsl:call-template name="replaceImage">
                    <xsl:with-param name="var" select="$cdata" />
            <xsl:when test="name() = 'variable'">
                <xsl:call-template name="insertVariable">
                    <xsl:with-param name="var" select="$cdata" />
                <xsl:element name="{name()}">
                    <xsl:copy-of select="@*" />
                    <xsl:call-template name="foreachOutput">
                        <xsl:with-param name="do-sect" select="./*" />
                        <xsl:with-param name="cdata" select="$cdata" />

  We have no "replace" or "variable" tags (which I do need to handle
differently than all the other tags), we are in the xsl:otherwise but I get
no text output only the tags and attributes of those tags. I need to output
all the text inside the tags (expect for the "variable" and "replace" tags.
Any clues?

  Thanks for any help!


----- Original Message -----
From: "Wendell Piez" <wapiez@xxxxxxxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Thursday, February 19, 2004 4:43 PM
Subject: Re: [xsl] Selecting and printing certain nodes

> Hi Glenn,
> At 04:02 PM 2/19/2004, you wrote:
> >I am having a problem printing certain nodes to the result tree. Here is
> >the XML I am working on:
> >
> >...
> ><tr><td><b>Before</b> After Variable</td/</tr>
> >...
> >
> >I have it to the point where I am printing the <tr>,<td> and <b> tags.
> >problem is the result is:
> ><tr><td><b></b></td></tr>  Where did all my text go?
> >
> >XSL:
> ><xsl:template name="foreachOutput">
> >     <xsl:param name="cdata"/>
> >     <xsl:param name="do-sect"/>
> >     <xsl:for-each select="$do-sect">
> >         <xsl:choose>
> >             <xsl:when test="name() = 'variable'">
> >                 <xsl:call-template name="insertVariable">
> >                     <xls:with-param name="var" select="$data"/>
> >                 </xsl:call-template>
> >             </xsl:when>
> >             <xsl:otherwise>
> >                 <xsl:element name="{name()}">
> >                     <xsl-copy-of select="@*"/>
> >                     <xsl:call-template name="foreachOutput">
> >                         <xsl:with-param name="do-sect" select="./*"/>
> >                         <xsl:with-param name="cdata" select="$cdata"/>
> >                     </xsl:call-template>
> >                 </xsl:element>
> >             </xsl:otherwise>
> >         </xsl:choose>
> >     <xsl:for-each>
> ></xsl:template>
> >
> > >From the above snipit of XML (<tr><td>...) I assume I am not getting
> >the <xsl:when test="name()='variable'"> section so I am in the
> ><xsl:otherwise> which uses the <xsl:element>
> That's a fair assumption. Since you don't have any <variable> elements,
> test="name()='variable'" will never be true.
> Beyond this, however, is a mystery. For one thing, as given your code
> work -- there's no assignment of a value to a variable $data; and it looks
> as though nothing will ever be assigned to $cdata either.
> Which really raises the question of why you are writing a recursive
> template to do what the XSL processor will do for you in any case. Is
> a reason why you aren't using the standard template-driven approach? If
> want your output to look just like your input, the identity template
> provides for this:
> <xsl:template match="node()">
>    <xsl:copy>
>      <xsl:copy-of select="@*"/>
>      <xsl:apply-templates/>
>    </xsl:copy>
> </xsl:template>
> (In this case, the text will be copied to output by virtue of matching the
> built-in template for text nodes, which copies the value of the node to
> result.)
> But even if you don't want your output exactly like your input,
> plain-vanilla XSLT template matching is far easier and more flexible, too.
> Please elucidate: we need more context for your code, both how it's
> invoked, and why you're doing it this way and not the easy way. :->
> Cheers,
> Wendell
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.      
> 17 West Jefferson Street                    Direct Phone: 301/315-9635
> Suite 207                                          Phone: 301/315-9631
> Rockville, MD  20850                                 Fax: 301/315-8285
> ----------------------------------------------------------------------
>    Mulberry Technologies: A Consultancy Specializing in SGML and XML
> ======================================================================
>  XSL-List info and archive:

 XSL-List info and archive:

Current Thread