我需要帮助修改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 subcategoryID
为0
,则仅在提及的categoryID
下显示结果子类别subcategoryID
,则结果应显示来自该特定子类别的值,该子类别位于所选子类别下categoryID
和subcategoryID
为0
,则显示所有结果传递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>
答案 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>
请注意使用变量以避免两次执行相同的计算。