在xslt中添加条件

时间:2011-08-07 17:28:28

标签: xslt

我需要帮助修改xslt中的条件。

我的xsl中有3个参数

<xsl:param name="today" select="'19470815'" /> <!-- this is date in yyyyMMdd format -->    
<xsl:param name="categoryID"/> <!-- This is CATEGORYID in xml file -->
<xsl:param name="subCategoryID"/> <!-- This is SUBCATEGORYID in xml file -->

我当前的xsl仅查找subcategory,但不会查找category

我的要求是:

  • 如果categoryID作为值0传递,则列出所有类别结果
  • 如果传递categoryID,则结果应仅显示该类别ID
  • 如果subcategoryID0,则仅在提及的categoryID下显示结果子类别
  • 如果传递了特定subcategoryID,则结果应显示来自该特定子类别的值,该子类别位于所选子类别下
  • 如果categoryIDsubcategoryID0,则显示所有结果

传递today参数,以便不显示过去的日期结果。

请指导我修复此xslt。我的xml和xslt位于

之下
<xsl:choose>
    <xsl:when test="$type = 'open' ">

        <xsl:for-each select="//SUMMARYNODE[SUMMARY/SUBCATEGORYID = $subCategoryID and SUMMARY/FORMATTEDDATE >= $today]  ">
            <xsl:sort select="SUMMARY/FORMATTEDDATE " />
            SOME STUFF
        </xsl:for-each>
        <xsl:if test="count(//SUMMARYNODE[SUMMARY/SUBCATEGORYID = $subCategoryID and SUMMARY/FORMATTEDDATE >= $today] ) >= 1 ">
            SOME MORE STUFF
        </xsl:if>
        <xsl:if test="not (count(//SUMMARYNODE[SUMMARY/SUBCATEGORYID = $subCategoryID and SUMMARY/FORMATTEDDATE >= $today] )>=1) ">
            NO RECORDS AVAILABLE
        </xsl:if>

    </xsl:when>

</xsl:choose>

XML输入

<root>

    <SUMMARYNODE>
        <SUMMARY>           
            <CATEGORY CATEGORYID="2">OPERATIONS PROCUREMENT</CATEGORY>
            <SUBCATEGORYID>4</SUBCATEGORYID>            
        </SUMMARY>

    </SUMMARYNODE>


    <SUMMARYNODE>
        <SUMMARY>           
            <CATEGORY CATEGORYID="2">XYZ</CATEGORY>
            <SUBCATEGORYID>6</SUBCATEGORYID>            
        </SUMMARY>

    </SUMMARYNODE>

    <SUMMARYNODE>
        <SUMMARY>           
            <CATEGORY CATEGORYID="5">ABC</CATEGORY>
            <SUBCATEGORYID>8</SUBCATEGORYID>            
        </SUMMARY>

    </SUMMARYNODE>

</root>

1 个答案:

答案 0 :(得分:1)

我相信你想要的条件如下

<xsl:for-each select="//SUMMARYNODE
    [SUMMARY/CATEGORY/@CATEGORYID = $categoryID or $categoryID = '0']
    [SUMMARY/SUBCATEGORYID = $subCategoryID or $subCategoryID = '0']
    [SUMMARY/FORMATTEDDATE >= $today]">

实际上,通常最好使用 xsl:apply-templates 而不是 xsl:for-each 。在这种情况下,仍然可以** xsl:sort。

这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:param name="today" select="'19470815'"/>
   <xsl:param name="categoryID" select="0"/>
   <xsl:param name="subCategoryID" select="0"/>

   <xsl:template match="/root">
      <xsl:apply-templates select="SUMMARYNODE
         [SUMMARY/CATEGORY/@CATEGORYID = $categoryID or $categoryID = '0']
         [SUMMARY/SUBCATEGORYID = $subCategoryID or $subCategoryID = '0']
         [SUMMARY/FORMATTEDDATE >= $today]">
         <xsl:sort select="SUMMARY/FORMATTEDDATE "/>
      </xsl:apply-templates>

      <xsl:variable name="recordCount" select="count(SUMMARYNODE         
         [SUMMARY/CATEGORY/@CATEGORYID = $categoryID or $categoryID = '0']
         [SUMMARY/SUBCATEGORYID = $subCategoryID or $subCategoryID = '0']
         [SUMMARY/FORMATTEDDATE >= $today])"/>
      <xsl:if test="$recordCount > 1"> SOME MORE STUFF </xsl:if>
      <xsl:if test="$recordCount = 0"> NO RECORDS AVAILABLE </xsl:if>
   </xsl:template>

   <xsl:template match="SUMMARYNODE">
    SOME STUFF 
   </xsl:template>
</xsl:stylesheet>

请注意使用变量以避免两次执行相同的计算。