Re: [xsl] [string to node]

Subject: Re: [xsl] [string to node]
From: ac <ac@xxxxxxxxxxxxx>
Date: Sun, 21 Feb 2010 17:52:26 -0500
Hi,

$a is a string as it is the result of <xsl:value-of select="//dd"/> which returns either the content of the first (xslt1) or the concatenated content of all dd elements (xslt2), for Test1. Test2 should have the same effect in both xslt 1 and 2, if I am not mistaken. In any case, to count the dd elements, you probably want to use count(//dd) or even count(d), in this instance, rather then count($a).

ac



This post is full of self-help realism, and completely lacks
explanation. That's for a reason -- I'm not near a reference work to
double check on the reason. :-)

Specifically in XALAN-C processor.
I also don't have ready access to the Xalan-C processor, but using
`xsltproc`, I found it instructive to put content into the<dd>
elements and examine the output of the following two stylesheets. The
first is just like yours (except the do-nothing conditional is
removed), and the second is the same but with apply-templates instead
of value-of.

Input
-----
<?xml version="1.0" encoding="UTF-8"?>
<a>
   <dd>one</dd>
   <dd>two</dd>
   <dd>three</dd>
</a>

Test1
-----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="xml" encoding="UTF-8"/>
   <xsl:template match="/">
     <xsl:variable name="a">
       <xsl:value-of select="//dd"/>
     </xsl:variable>
     <xsl:value-of select="count($a)"/>
     <xsl:text>: "</xsl:text>
     <xsl:value-of select="$a"/>
     <xsl:text>"</xsl:text>
   </xsl:template>
</xsl:stylesheet>

Test2
-----
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
   <xsl:output method="xml" encoding="UTF-8"/>
   <xsl:template match="/">
     <xsl:variable name="a">
       <xsl:apply-templates select="//dd"/>
     </xsl:variable>
     <xsl:value-of select="count($a)"/>
     <xsl:text>: "</xsl:text>
     <xsl:value-of select="$a"/>
     <xsl:text>"</xsl:text>
   </xsl:template>
</xsl:stylesheet>

Output1
-------
<?xml version="1.0" encoding="UTF-8"?>
1: "one"

Output2
-------
<?xml version="1.0" encoding="UTF-8"?>
1: "onetwothree"

In both cases it seems as though $a contains only 1 thing, a string.
(You can test that it is not a node set by using $a in the select= of
a for-each -- you should get a runtime error, since it is a string.)

So, my objective is to get the count() function to count the number of
'dd' elements.

test.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml" encoding="UTF-8"/>

<xsl:template match="/">
   <xsl:variable name="a">
    <xsl:choose>
     <xsl:when test="1=1">
          <xsl:value-of select="//dd"/>
     </xsl:when>
    </xsl:choose>
   </xsl:variable>
   <xsl:value-of select="count($a)"/>
</xsl:template>
</xsl:stylesheet>


test.xml


<a>
<dd/>
<dd/>
<dd/>
</a>

Current Thread