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
Wendell,

Thanks for the response! The following messages may shead some light on why
I am doing it this way.
http://www.biglist.com/lists/xsl-list/archives/200402/msg00826.html
http://www.biglist.com/lists/xsl-list/archives/200402/msg00831.html

Although my input no longer looks like:

<foreach>
<in>
<item>test1</item>
<item>test2</item>
<item>test3</item>
<item>test4</item>
<item>test5</item>
</in>
<do>
<tr><td><var/></td><td><var/></td></tr>
<tr><td><var/> Test</td><td><var/> Test</td></tr>
</do>
</foreach>

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>
</foreach>

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
through
<doc>
<item>dev1</item>
<item>dev2</item>
<item>dev3</item>
<item>dev4</item>
</doc>

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/*" />
  </xsl:call-template>
 </xsl:for-each>
</xsl:template>

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:choose>
            <xsl:when test="name() = 'replace'">
                <xsl:call-template name="replaceImage">
                    <xsl:with-param name="var" select="$cdata" />
                </xsl:call-template>
            </xsl:when>
            <xsl:when test="name() = 'variable'">
                <xsl:call-template name="insertVariable">
                    <xsl:with-param name="var" select="$cdata" />
                </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>

  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!

    Glenn





----- 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.
The
> >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
into
> >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,
the
> test="name()='variable'" will never be true.
>
> Beyond this, however, is a mystery. For one thing, as given your code
won't
> 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
there
> a reason why you aren't using the standard template-driven approach? If
you
> 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
the
> 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
getting
> invoked, and why you're doing it this way and not the easy way. :->
>
> Cheers,
> Wendell
>
>
> ======================================================================
> Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
> Mulberry Technologies, Inc.                http://www.mulberrytech.com
> 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:  http://www.mulberrytech.com/xsl/xsl-list
>
>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread