开发者

How to store node set in a variable based on choose-when condition

I have following XML structure

<pages>
    <page id="8992" filename="news7" extension=".aspx" title="News 7"
          real="True" virtual="False" visible="True" day="18" month="3"
          year="2010" />
    <page id="8991" filename="news6" extension=".aspx" title="News 6"
          real="True" virtual="False" visible="True" day="18" month="3"
          year="2010" />
    <page id="8990" filename="news5" extension=".aspx" title="News 5"
          real="True" virtual="False" visible="True" day="18" month="3"
          year="2010" />
    <page id="8883" filename="news2" extension=".aspx" title="News 2"
          real="True" virtual="False" visible="True" day="15" month="2"
          year="2010" />
    <page id="8989" filen开发者_运维百科ame="news4" extension=".aspx" title="News 4"
          real="True" virtual="False" visible="True" day="18" month="3"
          year="2009" />
</pages>

Now there is a variable

<xsl:variable name="valid_pages"/>

I want to store /pages/page in a variable based on following conditions

<xsl:variable name="valid_pages">
    <xsl:when test="count(/pages/page) &lt; 2">
        <xsl:value-of select="/pages/page[0]" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="/pages/page[position() &gt; 2]" />
    </xsl:otherwise>
</xsl:variable>

now when I use

<xsl:value-of select="count($valid_pages)" />

I get an error

To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set() function


Use:

<xsl:variable name="valid_pages" select=
"/pages/page[not(/pages/page[2])]
|
 /pages/page[position() > 2][/pages/page[2]]
"/>


First, this position() = 0 is false by definition.

Second, if you want some kind of partition with second as pivot, use

<xsl:variable name="valid_pages" 
              select="/pages/page[not(/pages/page[2])] |
                      /pages/page[position() > 2]"/>

Note: If there is no second, it won't be a third...


Result tree fragments like your valid_pages variable can be converted into a node-set by applying a processor-dependent function. XSLT 2.0 processors like Saxon9he will not need this, because in 2.0 RTF's are automatically interpreted as node-sets, but for XSLT 1.0 processors like MSXML 6.0 which you seem to be using, the following will work:

<xsl:value-of select="count(msxsl:node-set($valid_pages))" />

Another RTF to nodeset function that I know of is xalan:nodeset() for Xalan-J or -C.

Don't forget to include the namespace declaration in the stylesheet root element:
xmlns:msxsl="urn:schemas-microsoft-com:xslt" or
xmlns:xalan="http://xml.apache.org/xalan"

By the way the declaration of the valid_pages variable is wrong; not looking at significance and usefulness at all, it should at the least be written including xsl:choose as follows:

<xsl:variable name="valid_pages">
    <xsl:choose>
        <xsl:when test="count(/pages/page) &lt; 2">
            <xsl:value-of select="/pages/page[0]" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="/pages/page[position() &gt; 2]" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜