导航已排序的XML数据(XSLT)

时间:2010-05-10 17:52:11

标签: html xml xslt

我有一个包含公司数据的XML文件,适用于投资组合页面上8个行业的30家公司。用户可以选择按行业对此数据进行排序,并且将不断添加此XML文件。

使用<xsl:choose>在我的XSL文件中完成此排序。例如:

<xsl:when test="(invest[@investid='con'])">
    <xsl:for-each select="$invest-port/portfolio/company[@industry='Communications']">
                 <xsl:sort select="name" />
                 <div class="invest-port-thumb">
                     <a>
                        <xsl:attribute name="href">
                         <xsl:value-of select="link" />
                        </xsl:attribute>
                        </a>
                    </div>
                </xsl:for-each>
</xsl:when>

导航到单个公司的页面时,窗口底部有“上一个”和“下一个”按钮。我的问题是我需要这些动态链接到已排序的XML数据中的上一个和下一个<link>元素。

主XML文件的一部分:

<portfolio recact="1">

    <company industry="Industrial" status="Current" coid="1">
        <name>Horn Company</name>
        <hq>Owensboro, KY</hq>
        <link>horn.xml</link>
    </company>

    <company industry="Consumer" status="Current" coid="1">
        <name>Mike Waters Co</name>
        <hq>Orlando, FL</hq>
        <link>waters.xml</link>
    </company>

</portfolio>

这可能吗?或者有更简单的方法吗? (例如将每个公司放在行业划分的XML文件中而不是一个)

非常感谢任何见解!

1 个答案:

答案 0 :(得分:0)

使用preceding-siblingfollowing-sibling

修改

 <xsl:variable name="sorted">
      <xsl:for-each select="$invest-port/portfolio/company[@industry='Communications']">
           <xsl:sort select="name"/>
           <xsl:copy-of select="."/>
      </xsl:for-each>
 </xsl:variable>

然后你就可以了

<xsl:for-each select="$sorted/*">
    <xsl:apply-templates />
</xsl:for-each>

所以你可以使用previous-sibling和follow-sibling