如何在 xslt 中创建递归模板
How do I create recursive template in xslt
我有一个共享点列表,其设计如下。这是输入的示例数据。
Name ID Navigation_URL ParentID IsShow
Test1 1 # 0 Yes
Test2 2 # 0 Yes
Test2.1 3 # 2 Yes
Test2.1.14 # 3 Yes
如何使用 xslt 函数创建无序列表。
输出应该是这样的:
<ul>
<li>Test1</li>
<li>Test2
<ul>
<li>Test2.1
<ul>
<li>Test2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
我对 SharePoint 一无所知,我只能在 XSLT 部分帮助您。一旦你有一个 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>
以下样式表:
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>
将其转换为:
结果
<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
您可以再添加一个模板:
<xsl:template match="Item[IsShow='No']"/>
这是假设排除一个项目也应该排除它的后代。
我有一个共享点列表,其设计如下。这是输入的示例数据。
Name ID Navigation_URL ParentID IsShow
Test1 1 # 0 Yes
Test2 2 # 0 Yes
Test2.1 3 # 2 Yes
Test2.1.14 # 3 Yes
如何使用 xslt 函数创建无序列表。 输出应该是这样的:
<ul>
<li>Test1</li>
<li>Test2
<ul>
<li>Test2.1
<ul>
<li>Test2.1.1</li>
</ul>
</li>
</ul>
</li>
</ul>
我对 SharePoint 一无所知,我只能在 XSLT 部分帮助您。一旦你有一个 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>
以下样式表:
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>
将其转换为:
结果
<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
您可以再添加一个模板:
<xsl:template match="Item[IsShow='No']"/>
这是假设排除一个项目也应该排除它的后代。