内的XSLT条件为每个
如何在XSLT中的循环条件中包含if语句 我有2到3个不同的场景来定义我想要循环的数据。例如,可能有产品ID,也可能没有。如果有,我只想遍历包含该产品ID的记录,否则我想要所有记录。我试着这样做:内的XSLT条件为每个,xslt,Xslt,如何在XSLT中的循环条件中包含if语句 我有2到3个不同的场景来定义我想要循环的数据。例如,可能有产品ID,也可能没有。如果有,我只想遍历包含该产品ID的记录,否则我想要所有记录。我试着这样做: <xsl:variable name="knowledgebaseLoop"> <xsl:choose> <xsl:when test="string-length($productId) > 0"> &
<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>
<xsl:for-each select="item[@productId != '']">
但它无法解析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>
KnowledgebaseKnowledgebaseChange2测试2更改2测试2
PStrong变更2测试2br/
/强/平
10711076ASDASDESTEST测试22
ptst/p
1075测试
pTest/p
测试
我总是需要父ID为1073的文章。当且仅当产品ID存在时,我想要产品ID位于relatedProducts列表中的文章。再次感谢 您可以在foreach循环构造中执行检查,如下所示:
<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>
<xsl:for-each select="item[@productId != '']">
select中[]after项中的条件表示您只需要该属性与条件匹配的项。所以类似这样的东西会匹配:
<xsl:if test="@productID[.!='']"> //if it is not empty
<xsl:value-of select="@productID"/>
</xsl:if>
//如果它不是空的
例如,可能存在也可能不存在
产品ID。如果有,我想
仅循环通过包含
那个产品ID,否则我要全部
记录
我总是需要那些
父ID是1073。当且仅当
产品ID存在,我想要这些文章
产品ID在中的位置
相关产品
列表
使用:
<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>
我将使用
xsl:choose
(使用xsl:otherse
)和模板:
<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>
然后定义一个模板来处理节点列表。如果产品ID不存在,并且我根本不想将其包括在比较中,该怎么办?如何检查这两种情况?您可以使用第二个循环,如下所示:如果产品ID不存在,并且我根本不想将其包括在比较中,该怎么办?如何检查这两种情况?好问题,+1。请参阅我的答案,了解完整、简短且简单的一行XPath表达式解决方案。:)>“但是它无法解析XSLT。”-是否有错误消息?如果是这样的话,如果你告诉我们这个信息,会让每个人的生活更轻松,是吗?那好吧。这个消息非常有创意地说“无法解析Knowledgebase.xslt”。Dimitre-这是我解决这个问题的首选方法-我真的不想有多个模板,因为可能有多个情况,而且模板实际上是相同的,只是结果不同。但是,它不是parentId,我需要检查它的productId,相关产品可以是一个列表。我尝试了以下方法,但似乎不起作用:@Dexter:我很困惑——在您提供的代码中,您使用的是
$parentID
。这是个错误吗?您可能需要编辑问题并澄清/更正此问题。@Dimitre:不,没错,$parentID是我正在使用的变量之一$parentID将始终存在。我还需要检查$productId是否存在。这有意义吗?如果这个问题不清楚,我很抱歉。@Dexter:那么我不明白这个问题。请您提供一个示例XML文档(尽可能小),并描述结果必须是什么——最好也提供准确的解释结果?@Dimitre刚刚更新了一个示例,请告诉我这是否有帮助。再次感谢你的帮助!!