[英]XSLT Transforming XML into a cross referenced, nested HTML lists when source nodes are siblings and nesting is based on attribute values
Problem: I need to create a nested HTML unordered list from XML this is not nested. 问题:我需要从未嵌套的XML创建嵌套的HTML无序列表。 Additionally I need to cross reference the XML with an 'allowed nodes' section that is also contained in the document.
另外,我需要使用文档中也包含的“允许的节点”部分来交叉引用XML。
Example XML: XML示例:
<content>
<data>
<navigation>
<link name="about us" url="#"/>
<link name="staff" url="staff.asp" parent="about us"/>
<link name="contact" url="contact.asp" parent="about us"/>
<link name="facebook" url="facebook.asp"/>
</navigation>
</data>
<allowedlinks>
<link name="about us"/>
<link name="facebook"/>
</allowedlinks>
</content>
Example Result HTML (note I have left out the boiler plate code): 结果HTML示例(请注意,我省略了样板代码):
<ul>
<li>
about us
<ul>
<li>staff</li>
<li>contact</li>
</ul>
</li>
<li>facebook</li>
</ul>
Ultimately this will form a nav menu on a site. 最终,这将在网站上形成一个导航菜单。
Rules: -I need to use XSLT 1.0 to create a solution. 规则: -我需要使用XSLT 1.0创建解决方案。
-If a link exists, I need to add it to the UL and create any nested child UL's if any siblings contain a @parent of the current nodes @name. -如果存在链接,则如果任何兄弟姐妹包含当前节点的@parent,则我需要将其添加到UL并创建任何嵌套的子UL。
-Before generating the LI item of any node that does not have a @parent, it must first be confirmed that its @name matches a link @name in the allowed links section. -在生成没有@parent的任何节点的LI项目之前,必须首先确认其@name与允许的链接部分中的链接@name匹配。
In my opinion - the structure of the XML being transformed is really stupid and makes the transform process overly complex - however since I am unable to change the original XML, I need a solution. 我认为-转换的XML的结构确实很愚蠢,并且使转换过程过于复杂-但是,由于我无法更改原始XML,因此我需要一个解决方案。
Note - I do already have an answer which works okay, I will post it shortly. 注意-我已经有了一个行之有效的答案,我会尽快发布。 I wanted to see other possible answers first :)
我想先看看其他可能的答案:)
Bonus if this can be done with template matching and not too many for each loops. 如果可以通过模板匹配来完成此操作,并且每次循环的操作次数都不太多,则可以加分。
My solution contains 2 nested for-each's which I do not like. 我的解决方案包含2个我不喜欢的嵌套for-for。
This is a classic case for using keys : 这是使用键的经典案例:
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="allowed-link" match="allowedlinks/link" use="@name" />
<xsl:key name="link-by-parent" match="link" use="@parent" />
<xsl:template match="/content">
<ul>
<xsl:apply-templates select="data/navigation/link[not(@parent) and key('allowed-link', @name)]"/>
</ul>
</xsl:template>
<xsl:template match="link">
<li>
<xsl:value-of select="@name"/>
<xsl:variable name="sublinks" select="key('link-by-parent', @name)" />
<xsl:if test="$sublinks">
<ul>
<xsl:apply-templates select="$sublinks"/>
</ul>
</xsl:if>
</li>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.