xsl position()函数 lt and gt 问题
我的sitemap.xml如下:
<node> <title>Home </title> <link> <a href="/CMS400Developer/default.aspx" alt="Medical Home">Home </a> </link> <description>Medical Home </description> <node> <title>About Us </title> <link> <a href="/CMS400Developer/aboutus.aspx" alt="About Medical">About Us </a> </link> <description>About Medical </description> <node> <title>Staff </title> <link> <a href="/CMS400Developer/staff.aspx" alt="Staff list">Staff </a> </link> <description>Staff list </description> </node> <node> <title>News </title> <link> <a href="/CMS400Developer/news.aspx" alt="News">News </a> </link> <description>News </description> </node> <node> <title>Careers </title> <link> <a href="/CMS400Developer/jobs.aspx" alt="Careers at Medical Center">Careers </a> </link> <description>Careers at Medical Center </description> </node> <node> <title>Calender </title> <link> <a href="/CMS400Developer/events.aspx" alt="">Calender </a> </link> <description> </description> </node> </node> <node> <title>Services </title> <link> <a href="/CMS400Developer/services.aspx" alt="Services">Services </a> </link> <description>Services </description> </node> <node> <title>Products </title> <link> <a href="" alt="Product page">Products </a> </link> <description>Product page </description> </node> </node>
现在的xsl文件如下。如果写成]<xsl:if test="position() < $folderCount+2">,可以打印出子目录,没有任何问题。只是当我将lt 改成 gt 后,就只能打出主目录,其对应的子目录就输出不了了。不知道这是什么原因造成的,请各位帮忙!多谢!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="folderCount" select="round( count(/node/node) div 2)"/>
<xsl:template match="/node">
<div id="columnOne">
This is the first column
</div>
<div id="columnTwo">
<ul class="sitemap">
<li id="firstItem" class="primary">
<a>
<xsl:attribute name="href">
<xsl:value-of select="link/a/@href"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</li>
<xsl:apply-templates select="node"></xsl:apply-templates>
</ul>
</div>
</xsl:template>
<xsl:template match="node">
<xsl:if test="position() > $folderCount+2">
<li class="primary">
<xsl:choose>
<xsl:when test="link/a">
<xsl:choose>
<xsl:when test="link/a[@href!='']">
<a>
<xsl:attribute name="href">
<xsl:value-of select="link/a/@href"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="title"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="not(link/a)">
<xsl:value-of select="title"/>
</xsl:when>
</xsl:choose>
</li>
<xsl:for-each select="./node">
<ul class="tertiary">
<xsl:apply-templates select="."></xsl:apply-templates>
</ul>
</xsl:for-each>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
[解决办法]
position() > $folderCount+2 的话没有符合的节点,
以上XML中 $folderCount = 2
而position()的值为1,2,3, 都不符合 position() > $folderCount+2
[解决办法]
- XML code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="folderCount" select="count(/node/node) div 2"/>
<xsl:template match="/node">
<div id="columnOne">
This is the first column
</div>
<div id="columnTwo">
<ul class="sitemap">
<li id="firstItem" class="primary">
<a>
<xsl:attribute name="href">
<xsl:value-of select="link/a/@href"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</li>
<xsl:apply-templates select="node[position() > $folderCount]"> </xsl:apply-templates>
</ul>
</div>
</xsl:template>
<xsl:template match="node">
<li class="primary">
<xsl:choose>
<xsl:when test="link/a">
<xsl:choose>
<xsl:when test="link/a[@href!='']">
<a>
<xsl:attribute name="href">
<xsl:value-of select="link/a/@href"/>
</xsl:attribute>
<xsl:value-of select="title"/>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="title"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="not(link/a)">
<xsl:value-of select="title"/>
</xsl:when>
</xsl:choose>
</li>
<xsl:for-each select="./node">
<ul class="tertiary">
<xsl:apply-templates select="."> </xsl:apply-templates>
</ul>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>