我正在尝试将我的注意力集中在 xslt 上。
stackoverflow 帮助中的一些问题(XSLT 模板和递归 https://stackoverflow.com/questions/2835567/xslt-templates-and-recursion
and
XSLT for-each 循环,基于变量的过滤器 https://stackoverflow.com/questions/5709024/xslt-for-each-loop-filter-based-on-variable)但我还是有点困惑。
我想我“将模板视为函数”
(https://stackoverflow.com/questions/506348/how-do-i-know-my-xsl-is-efficient-and-beautiful https://stackoverflow.com/questions/506348/how-do-i-know-my-xsl-is-efficient-and-beautiful )
无论如何...我的数据是
<Entities>
<Entity ID="8" SortValue="0" Name="test" ParentID="0" />
<Entity ID="14" SortValue="2" Name="test2" ParentID="8" />
<Entity ID="16" SortValue="1" Name="test3" ParentID="8" />
<Entity ID="17" SortValue="3" Name="test4" ParentID="14" />
<Entity ID="18" SortValue="3" Name="test5" ParentID="0" />
</Entities>
我想要的输出基本上是一个“树视图”
<ul>
<li id="entity8">
test
<ul>
<li id="entity16">
test3
</li>
<li id="entity14">
test2
<ul>
<li id="entity17">
test4
</li>
</ul>
</li>
</ul>
</li>
<li id="entity18">
test5
</li>
</ul>
到目前为止,我的 XSLT 是错误的,因为它肯定“将模板视为函数”,并且在执行时还会抛出 StackOverflowException (:-))
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="Entities">
<ul>
<li>
<xsl:value-of select="local-name()"/>
<xsl:apply-templates/>
</li>
</ul>
</xsl:template>
<xsl:template match="//Entities/Entity[@ParentID=0]">
<xsl:call-template name="recursive">
<xsl:with-param name="parentEntityID" select="0"></xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="recursive">
<xsl:param name="parentEntityID"></xsl:param>
<xsl:variable name="counter" select="//Entities/Entity[@ParentID=$parentEntityID]"></xsl:variable>
<xsl:if test="count($counter) > 0">
<xsl:if test="$parentEntityID > 0">
</xsl:if>
<li>
<xsl:variable name="entityID" select="@ID"></xsl:variable>
<xsl:variable name="sortValue" select="@SortValue"></xsl:variable>
<xsl:variable name="name" select="@Name"></xsl:variable>
<xsl:variable name="parentID" select="@ParentID"></xsl:variable>
<a href=?ID={$entityID}&ParentEntityID={$parentID}">
<xsl:value-of select="$name"/>
</a>
<xsl:call-template name="recursive">
<xsl:with-param name="parentEntityID" select="$entityID"></xsl:with-param>
</xsl:call-template>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
我知道如何通过代码来做到这一点,没问题。
不过,这一次,我正在寻找 xslt 中的解决方案,对此任何帮助将不胜感激。
虽然call-template
如果您发现自己更喜欢命名模板,那么命名模板是该语言的一个非常有用的功能apply-templates
这可能表明您仍在思考函数而不是模板。如果您在命名模板中所做的第一件事是选择要操作的节点集,则尤其如此。
这是您正在尝试执行的操作的简单版本。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<ul>
<xsl:apply-templates select="Entities/Entity[@ParentID=0]" />
</ul>
</xsl:template>
<xsl:template match="Entity">
<li>
<xsl:value-of select="@Name" />
<xsl:apply-templates select="../Entity[@ParentID=current()/@ID]" />
</li>
</xsl:template>
</xsl:stylesheet>
请注意,不需要计数器,因为“父”的值提供了必要的上下文。
另请注意,所有Entities
行为方式相同,无论它们在树中的哪个位置,它们都包含它们的@Name
值,并将模板应用于任何Entity
@ParentID 与当前级别的 @ID 匹配的对象。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)