How can I include an if statement in my loop conditional in XSLT?
I have 2 or 3 different scenarios that define the data I want to loop through. For example, there may or may not be a product ID. If there is, I want to only loop through records that contain that product ID, otherwise I want all records. I was trying to do something like this:
<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>
But it's unable to parse the XSLT. I know the 2 conditions are the same, I was going to rewrite one once I got it working. Thanks in advance for any help!
UPDATE:
Here is some sample 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开发者_如何学JAVA</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>
I always need the articles where the parent ID is 1073. If and only if a product ID exists, I want the articles where the product ID is in the relatedProducts list. Thanks again!
For example, there may or may not be a product ID. If there is, I want to only loop through records that contain that product ID, otherwise I want all records.
I always need the articles where the parent ID is 1073. If and only if a product ID exists, I want the articles where the product ID is in the
relatedProducts
list.
Use:
<xsl:variable name="vDesiredArticles" select=
"/*/Article[@parentID='1073'
or
contains(concat(',',relatedProducts,','),
concat(',', $pProductId, ',')
)
]"/>
Here is a complete and short example:
<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>
when this transformation is applied on the following XML document (based on the provided one in the Update, but made more interesting):
<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>
the wanted, correct result is produced (only Articles with parentID="1073"
or whose relatedProducts
contains the$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>
I would use an xsl:choose
(to get the else part with a xsl:otherwise
) plus a template:
<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>
And then define a template to process the node list.
You can do the check in the foreach loop construct, like this:
<xsl:for-each select="item[@productId != '']">
The condtion in the [] after item in the select indicate that you only want items with that attribute matching the condition. So something like this would match: < item productId="BR9ES" >
But this would not: < item >
This is what I use to test if an element exists in my XML doc:
<xsl:if test="@productID[.!='']"> //if it is not empty
<xsl:value-of select="@productID"/>
</xsl:if>
精彩评论