如何在循环条件中在XSLT中包含if语句?
我有2或3个不同的场景定义了我想要循环的数据。例如,可能有也可能没有产品ID。如果有,我想只循环包含该产品ID的记录,否则我想要所有记录。我试图做这样的事情:
<xsl:variable name="knowledgebaseLoop">
<xsl:choose>
<xsl:when test="string-length($productId) > 0">
<xsl:copy-of select="$currentPage/child::* [@parentID = $parentId]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="$currentPage/child::* [@parentID = $parentId]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:for-each select="knowledgebaseLoop">
<xsl:if test="position() > $recordsPerPage * number($pageNumber - 1) and position() <= number($recordsPerPage * number($pageNumber - 1) + $recordsPerPage )">
<div id="main_body_listing">
<div class="listing_title2"><a href="{umbraco.library:NiceUrl(current()/@id)}"><xsl:value-of select="current()/@nodeName"/></a></div>
<xsl:value-of select="current()/summary"/>
</div>
</xsl:if>
</xsl:for-each>
但它无法解析XSLT。我知道两个条件是相同的,一旦我开始工作,我就会重写一个。在此先感谢您的帮助!
更新:
以下是一些示例XML:
<Knowledgebase id="1073" parentID="-1" level="1" writerID="0" creatorID="0" nodeType="1062" template="1083" sortOrder="5" createDate="2011-04-20T13:43:41" updateDate="2011-05-11T14:34:23" nodeName="Knowledgebase" urlName="knowledgebase" writerName="Chris" creatorName="Chris" path="-1,1073" isDoc=""><pageTitle>Knowledgebase</pageTitle><metaDescription>knowledgebase</metaDescription><metaKeywords>knowledgebase</metaKeywords><umbracoUrlName /><Article id="1074" parentID="1073" level="2" writerID="0" creatorID="0" nodeType="1063" template="1084" sortOrder="1" createDate="2011-04-20T13:44:13" updateDate="2011-05-16T09:39:12" nodeName="What is the answer to this question?" urlName="what-is-the-answer-to-this-question" writerName="Chris" creatorName="Chris" path="-1,1073,1074" isDoc=""><title>Change 2 Test 2</title><summary>Change 2 Test 2</summary><description>
<p><strong>Change 2 Test 2<br />
</strong></p>
</description><relatedProducts>1071,1076</relatedProducts><pageTitle>asd</pageTitle><metaDescription>asd</metaDescription><metaKeywords>asd</metaKeywords><umbracoUrlName /></Article><Article id="1082" parentID="1073" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc=""><title>test</title><summary>test 22</summary><description>
<p>tst</p>
</description><relatedProducts>1075</relatedProducts><pageTitle /><metaDescription></metaDescription><metaKeywords></metaKeywords><umbracoUrlName /></Article><Article id="1146" parentID="1073" level="2" writerID="4" creatorID="3" nodeType="1063" template="1084" sortOrder="3" createDate="2011-05-13T15:36:14" updateDate="2011-05-13T15:37:21" nodeName="Question 3" urlName="question-3" writerName="Content Approver" creatorName="Content Writer" path="-1,1073,1146" isDoc=""><title>Test</title><summary></summary><description>
<p>Test</p>
</description><relatedProducts /><pageTitle>Test</pageTitle><metaDescription>test</metaDescription><metaKeywords>test</metaKeywords><umbracoUrlName /></Article></Knowledgebase>
我总是需要父ID为1073的文章。当且仅当存在产品ID时,我想要产品ID在relatedProducts列表中的文章。再次感谢!
答案 0 :(得分:3)
例如,可能有也可能没有 产品ID。如果有,我想 只循环包含的记录 那个产品ID,否则我想要所有 记录。
我总是需要那些文章 父ID是1073.当且仅当a 产品ID存在,我想要文章 产品ID在哪里
relatedProducts
列表。
使用强>:
<xsl:variable name="vDesiredArticles" select=
"/*/Article[@parentID='1073'
or
contains(concat(',',relatedProducts,','),
concat(',', $pProductId, ',')
)
]"/>
以下是一个完整且简短的示例:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pProductId" select="1077"/>
<xsl:template match="/">
<xsl:variable name="vDesiredArticles" select=
"/*/Article[@parentID='1073'
or
contains(concat(',',relatedProducts,','),
concat(',', $pProductId, ',')
)
]"/>
<xsl:copy-of select="$vDesiredArticles"/>
</xsl:template>
</xsl:stylesheet>
将此转换应用于以下XML文档(基于更新中提供的转换,但更有趣):
<Knowledgebase id="1073" parentID="-1" level="1" writerID="0"
creatorID="0" nodeType="1062" template="1083" sortOrder="5"
createDate="2011-04-20T13:43:41" updateDate="2011-05-11T14:34:23"
nodeName="Knowledgebase" urlName="knowledgebase" writerName="Chris"
creatorName="Chris" path="-1,1073" isDoc="">
<pageTitle>Knowledgebase</pageTitle>
<metaDescription>knowledgebase</metaDescription>
<metaKeywords>knowledgebase</metaKeywords>
<umbracoUrlName />
<Article id="1074" parentID="1073" level="2"
writerID="0" creatorID="0" nodeType="1063" template="1084"
sortOrder="1" createDate="2011-04-20T13:44:13"
updateDate="2011-05-16T09:39:12"
nodeName="What is the answer to this question?"
urlName="what-is-the-answer-to-this-question" writerName="Chris"
creatorName="Chris" path="-1,1073,1074" isDoc="">
<title>Change 2 Test 2</title>
<summary>Change 2 Test 2</summary>
<description> <p><strong>Change 2 Test 2<br /> </strong></p> </description>
<relatedProducts>1071,1076</relatedProducts>
<pageTitle>asd</pageTitle>
<metaDescription>asd</metaDescription>
<metaKeywords>asd</metaKeywords>
<umbracoUrlName />
</Article>
<Article id="1082" parentID="1073" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc="">
<title>test</title>
<summary>test 22</summary>
<description> <p>tst</p> </description>
<relatedProducts>1075</relatedProducts>
<pageTitle />
<metaDescription></metaDescription>
<metaKeywords></metaKeywords>
<umbracoUrlName />
</Article>
<Article id="1088" parentID="1077" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc="">
<title>test</title>
<summary>test 22</summary>
<description> <p>tst</p> </description>
<relatedProducts>1075,1039,1077</relatedProducts>
<pageTitle />
<metaDescription></metaDescription>
<metaKeywords></metaKeywords>
<umbracoUrlName />
</Article>
<Article id="1089" parentID="1077" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc="">
<title>test</title>
<summary>test 22</summary>
<description> <p>tst</p> </description>
<relatedProducts>1075,1039,1078</relatedProducts>
<pageTitle />
<metaDescription></metaDescription>
<metaKeywords></metaKeywords>
<umbracoUrlName />
</Article>
<Article id="1146" parentID="1073" level="2" writerID="4" creatorID="3" nodeType="1063" template="1084" sortOrder="3" createDate="2011-05-13T15:36:14" updateDate="2011-05-13T15:37:21" nodeName="Question 3" urlName="question-3" writerName="Content Approver" creatorName="Content Writer" path="-1,1073,1146" isDoc="">
<title>Test</title>
<summary></summary>
<description> <p>Test</p> </description>
<relatedProducts />
<pageTitle>Test</pageTitle>
<metaDescription>test</metaDescription>
<metaKeywords>test</metaKeywords>
<umbracoUrlName />
</Article>
</Knowledgebase>
生成了需要的,正确的结果(只有包含parentID="1073"
或relatedProducts
包含$pProductId
(1077)的文章):
<Article id="1074" parentID="1073" level="2" writerID="0" creatorID="0" nodeType="1063" template="1084" sortOrder="1" createDate="2011-04-20T13:44:13" updateDate="2011-05-16T09:39:12" nodeName="What is the answer to this question?" urlName="what-is-the-answer-to-this-question" writerName="Chris" creatorName="Chris" path="-1,1073,1074" isDoc="">
<title>Change 2 Test 2</title>
<summary>Change 2 Test 2</summary>
<description> <p><strong>Change 2 Test 2<br /> </strong></p> </description>
<relatedProducts>1071,1076</relatedProducts>
<pageTitle>asd</pageTitle>
<metaDescription>asd</metaDescription>
<metaKeywords>asd</metaKeywords>
<umbracoUrlName/>
</Article>
<Article id="1082" parentID="1073" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc="">
<title>test</title>
<summary>test 22</summary>
<description> <p>tst</p> </description>
<relatedProducts>1075</relatedProducts>
<pageTitle/>
<metaDescription/>
<metaKeywords/>
<umbracoUrlName/>
</Article>
<Article id="1088" parentID="1077" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc="">
<title>test</title>
<summary>test 22</summary>
<description> <p>tst</p> </description>
<relatedProducts>1075,1039,1077</relatedProducts>
<pageTitle/>
<metaDescription/>
<metaKeywords/>
<umbracoUrlName/>
</Article>
<Article id="1146" parentID="1073" level="2" writerID="4" creatorID="3" nodeType="1063" template="1084" sortOrder="3" createDate="2011-05-13T15:36:14" updateDate="2011-05-13T15:37:21" nodeName="Question 3" urlName="question-3" writerName="Content Approver" creatorName="Content Writer" path="-1,1073,1146" isDoc="">
<title>Test</title>
<summary/>
<description> <p>Test</p> </description>
<relatedProducts/>
<pageTitle>Test</pageTitle>
<metaDescription>test</metaDescription>
<metaKeywords>test</metaKeywords>
<umbracoUrlName/>
</Article>
答案 1 :(得分:1)
我会使用xsl:choose
(使用xsl:otherwise
获取其他部分)以及模板:
<xsl:choose>
<xsl:when test='@productId'>
<xsl:apply-templates select='filter by product id' />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select='all of them' />
</xsl:otherwise>
然后定义一个模板来处理节点列表。
答案 2 :(得分:0)
您可以在foreach循环结构中进行检查,如下所示:
<xsl:for-each select="item[@productId != '']">
选择后面的[]后面的条件表示您只需要具有与该条件匹配的属性的项目。所以像这样的东西会匹配: &LT; item productId =“BR9ES”&gt;
但这不会: &LT; item&gt;
答案 3 :(得分:0)
这是我用来测试XML文档中是否存在元素的内容:
<xsl:if test="@productID[.!='']"> //if it is not empty
<xsl:value-of select="@productID"/>
</xsl:if>