xsl-list
[Top] [All Lists]

Re: Selecting and printing certain nodes

2004-02-20 09:31:04
You guy's are great! Thanks so much for the help. I went on David and
Wendell's suggestions, it simplified it quite a bit and it works exactly as
I want. This is great!

I have a couple more questions...sorry...

XML snipit:
<variable name="$startDate"/>

XSL snipit:
<xsl:template match="variable">
    <xsl:if test="@name"/>
        <xsl:value-of select="@name"/>
    </xsl:if>
</xsl:template>

This prints out '$startDate' when I really want it to print the value of the
xsl:param $startDate. Is there a way to do this in XSL?

So let me explain my situation with some examples:

XML input file:
<html>
<head><title>This is the title of the document</title></head>
<body>
<table border="1" cellpadding="0">
<tr><td colspan="2"><replace with="tag">title</replace></td></tr>
<tr><td colspan="2">&nbsp;</td></tr>
<foreach param="devices">
<tr>
<td>The device is <variable> Param is P1</td>
<td>The device is <variable> Param is P2</td>
</tr>
</foreach>
</body>
</html>

So the replace just replaces itself with the text of the title name, no
problem there I use the exslt dyn:evaluate function for that.
Next is the foreach. This works by the stylesheet opening the 'devices.xml'
file using the document function and reading out the /doc/items

<xsl:template match="foreach">
<xsl:param name="var"/>
<xsl:variable name="here" select="."/>
<xsl:for-each select="document(@param)/doc/item">
<div>
<xsl:apply-templates select="$here/node()">
<xsl:with-param name="var" select="."/>
</xsl:apply-templates>
</div>
</xsl:for-each>
</xsl:template>

<xsl:template match="variable">
<xsl:param name="var"/>
<xsl:value-of select="$var"/>
</xsl:template>

This all work great. Now my question is can you think of a way to handle
nested foreach statments?

<foreach param="devices">
<tr><td colspan="2"><variable/></td></tr>
<foreach param="parameters">
<tr><td><variable/></td></tr>
</foreach>
</foreach>

Do I need to pass a variable which is an array or something

Thanks

        Glenn


----- Original Message -----
From: "David Carlisle" <davidc(_at_)nag(_dot_)co(_dot_)uk>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Thursday, February 19, 2004 6:34 PM
Subject: Re: [xsl] Selecting and printing certain nodes



         <xsl:with-param name="do-sect" select="$foreachNode/*" />

You threw away all the text at that pont, as * selects element nodes
(only) If you want all child nodes use /node() not /* at the end.

It still seems highly likely that a much more simple apply-templates
based solution is possibe rather than an explicitly recursive template.


side issue, usual complaint about misuse of word tag

  I need to output all the text inside the tags

It seems every one misuses tag like this but I'm not sure why. tag has a
very specific meaning, and tags are opened with < (what used to be
called STAGO: start tag open character) and closed with > (what used to
be called STAGC) the stuff inside a tag consists of the element name,
some attribute markup and some ignorable white space. You want the text
that is explictly _not_ inside the tags. (I know this seems pedantry,
but it really does make it harder to understand the problem when people
say exactly the oposite of what they mean)

  I need to output all the text inside the tags (expect for the
  "variable" and "replace" tags.

Why can't you just use the identity template, as Wendell suggested,
together with a couple of templates for the variable and replace
_elements_.?

"cdata" is also a slightly confusing name for the param as CDATA also
has a technical meaning but (in XML but not SGML) CDATA declared text
can only appear in attribute values, not in element content.



David

I'm not sure I fully understood your requirement but this copies your
foreach stuff 4 times, replacing <var/> with a dev 1... each time.


var1.xml
<doc>
<item>dev1</item>
<item>dev2</item>
<item>dev3</item>
<item>dev4</item>
</doc>


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


var3.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
<xsl:template match="*">
<xsl:param name="var"/>
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates>
  <xsl:with-param name="var" select="$var"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>

<xsl:template match="foreach">
<xsl:param name="var"/>
<xsl:variable name="here" select="."/>
<xsl:for-each select="document('var1.xml')/doc/item">
<div>
<xsl:apply-templates select="$here/node()">
  <xsl:with-param name="var" select="."/>
</xsl:apply-templates>
</div>
</xsl:for-each>
</xsl:template>

<xsl:template match="var">
<xsl:param name="var"/>
<span><xsl:value-of select="$var"/></span>
</xsl:template>
</xsl:stylesheet>



$ saxon var2.xml var3.xsl
<?xml version="1.0" encoding="utf-8"?><div>
<tr><td><b>BOLDFACE</b> Some <span>dev1</span>
Text</td><td>Here</td></tr>
<tr><td> Test11</td><td>Test</td></tr>
</div><div>
<tr><td><b>BOLDFACE</b> Some <span>dev2</span>
Text</td><td>Here</td></tr>
<tr><td> Test11</td><td>Test</td></tr>
</div><div>
<tr><td><b>BOLDFACE</b> Some <span>dev3</span>
Text</td><td>Here</td></tr>
<tr><td> Test11</td><td>Test</td></tr>
</div><div>
<tr><td><b>BOLDFACE</b> Some <span>dev4</span>
Text</td><td>Here</td></tr>
<tr><td> Test11</td><td>Test</td></tr>
</div>



--
http://www.dcarlisle.demon.co.uk/matthew

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




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