[英]How do I create recursive template in xslt
I have a sharepoint list which is designed like below. 我有一个共享点列表,其设计如下。 Here is a sample data of input.
这是输入的样本数据。
Name ID Navigation_URL ParentID IsShow
Test1 1 # 0 Yes
Test2 2 # 0 Yes
Test2.1 3 # 2 Yes
Test2.1.14 # 3 Yes
How to create an unordered list using xslt function. 如何使用xslt函数创建无序列表。 Output should be like:
输出应为:
<ul>
<li>Test1</li>
<li>Test2
<ul>
<li>Test2.1
<ul>
<li>Test2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
I know nothing about SharePoint, I can only help you with the XSLT part. 我对SharePoint一无所知,我只能在XSLT部分为您提供帮助。 Once you have an XML document such as:
一旦有了XML文档,例如:
XML XML格式
<root>
<Item>
<Name>Test1</Name>
<ID>1</ID>
<ParentID>0</ParentID>
</Item>
<Item>
<Name>Test2</Name>
<ID>2</ID>
<ParentID>0</ParentID>
</Item>
<Item>
<Name>Test2.1</Name>
<ID>3</ID>
<ParentID>2</ParentID>
</Item>
<Item>
<Name>Test2.1.1</Name>
<ID>4</ID>
<ParentID>3</ParentID>
</Item>
</root>
the following stylesheet: 以下样式表:
XSLT 1.0 XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="item-by-parent" match="Item" use="ParentID" />
<xsl:template match="/root">
<ul>
<xsl:apply-templates select="Item[ParentID='0']"/>
</ul>
</xsl:template>
<xsl:template match="Item">
<xsl:variable name="children" select="key('item-by-parent', ID)" />
<li>
<xsl:value-of select="Name"/>
<xsl:if test="$children">
<ul>
<xsl:apply-templates select="$children"/>
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
will transform it to: 将其转换为:
Result 结果
<ul>
<li>Test1</li>
<li>Test2<ul>
<li>Test2.1<ul>
<li>Test2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
One thing is missed isShow ... It's a node/column based on which I can exclude or not show that corresponding item
错过了一件事情isShow ...这是一个节点/列,我可以根据该节点/列排除或不显示相应的项目
You could just add another template: 您可以添加另一个模板:
<xsl:template match="Item[IsShow='No']"/>
This is assuming that excluding an item is also supposed to exclude its descendants. 这是假设排除一个项目也应该排除其后代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.