尝试汇编正确的XSLT代码,以将从递归链的最低成员(从其"uniq-id"(获取的公共ID分配给上面的所有元素。无法获取祖先的节点。
源 XML
<root>
<Object id="top" uniq-id="001" status="0" rank="1"/>
<Object id="middle" id-parent="top" uniq-id="020" status="0" rank="3"/>
<Object id="middle" id-parent="top" uniq-id="021" status="1" rank="3"/>
<!-- only the element with status="0" is considered -->
<Object id="bottom-1" id-parent="middle" uniq-id="111" status="1" rank="6" />
<Object id="bottom-2" id-parent="middle" uniq-id="222" status="1" rank="6" />
<Object id="bottom-3" id-parent="middle" uniq-id="333" status="0" rank="6" /> <!-- will be taken -->
<Object id="bottom-4" id-parent="middle" uniq-id="444" status="1" rank="6" />
<Object id="bottom-5" id-parent="middle" uniq-id="555" status="1" rank="7" />
<Object id="bottom-6" id-parent="middle" uniq-id="666" status="0" rank="7" /> <!-- will be taken -->
<Object id="bottom-7" id-parent="middle" uniq-id="777" status="1" rank="6" />
<Object id="bottom-8" id-parent="middle" uniq-id="888" status="1" rank="6" />
<Object id="bottom-9" id-parent="middle" uniq-id="999" status="0" rank="6" /> <!-- will be taken -->
</root>
启动-XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common">
<xsl:key name="Object-By-id" match="Object[(@status='0')]" use="@id"/>
<xsl:key name="Object-By-id-parent" match="Object[(@status='0')]" use="string(@id-parent)"/>
<xsl:variable name="fold-rtf">
<xsl:apply-templates select="/" mode="fold"/>
</xsl:variable>
<xsl:variable name="folded-tree" select="exslt:node-set($fold-rtf)"/>
<xsl:template match="@*|node()[(@status='0')]">
<xsl:copy>
<xsl:apply-templates select="@* | node()[(@status='0')]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Object[@status=0]/@*[last()]">
<xsl:variable name="current" select=".."/>
<xsl:copy/>
<xsl:for-each select="$folded-tree">
<xsl:for-each select="key('Object-By-id',$current/@id)">
<!-- ====== !! =======-->
<xsl:if test="position()!=last()">
<xsl:for-each select="ancestor-or-self::*">
<xsl:attribute name="chain-id">
<xsl:value-of select="@uniq-id"/>
</xsl:attribute>
</xsl:for-each>
</xsl:if>
<!-- ======= !! =======-->
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="/|*" mode="fold">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="key('Object-By-id-parent',string(@id))" mode="fold">
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果(不正确(https://xsltfiddle.liberty-development.net/94Acsm2/1
假定输出
<root>
<Object id="top" uniq-id="001" status="0" rank="1" chain-id="20"/>
<Object id="middle" id-parent="top" uniq-id="020" status="0" rank="3" chain-id="20"/>
<Object id="bottom-3" id-parent="middle" uniq-id="333" status="0" rank="6" chain-id="333"/>
<Object id="middle" id-parent="top" uniq-id="020" status="0" rank="3" chain-id="333"/>
<Object id="top" uniq-id="001" status="0" rank="1" chain-id="333"/>
<Object id="bottom-6" id-parent="middle" uniq-id="666" status="0" rank="7" chain-id="666"/>
<Object id="middle" id-parent="top" uniq-id="020" status="0" rank="3" chain-id="666"/>
<Object id="top" uniq-id="001" status="0" rank="1" chain-id="666"/>
<Object id="bottom-9" id-parent="middle" uniq-id="999" status="0" rank="6" chain-id="999"/>
<Object id="middle" id-parent="top" uniq-id="020" status="0" rank="3" chain-id="999"/>
<Object id="top" uniq-id="001" status="0" rank="1" chain-id="999"/>
</root>
欢迎您提供所有如何改进 XSLT 代码的解决方案
从非工作代码中逆向工程您的需求总是很困难的,但我认为您要做的是选择带有@status="0"
的元素,并且对于其中的每一个,显示逻辑祖先,即关系parent(X)
的递归展开,其中parent(X)
选择@id等于X/@parent-id
的元素。
自从我用 XSLT 1.0 编码以来已经有很长时间了(它已经被取代了很长时间......(,所以如果我犯了任何错误,请道歉。
首先定义一个按 id 选择对象的键:
<xsl:key name="by-id" match="Object" use="@id"/>
现在定义一个递归模板来打印节点的逻辑祖先:
<xsl:template match="Object" mode="ancestors">
<xsl:copy-of select="."/>
<xsl:apply-templates select="key('by-id', @parent-id)" mode="ancestors"/>
</xsl:template>
现在为所选项目调用此函数:
<xsl:template match="/">
<root>
<xsl:apply-templates select="root/Object[@status='0']" mode="ancestors"/>
</root>
</xsl:template>
未经测试。