【发布时间】:2013-08-27 09:13:02
【问题描述】:
我有这个 XML:
<TreeView>
<Parent text="Installation">
<child company="all" text="Startup" type="video" file="startup.mp4"/>
<child company="all" text="Getting there" type="video" file="small.mp4"/>
<child company="all" text="Steps" type="pdf_document" file="test.pdf"/>
<child company="all" text="Pictures" type="presentation" file="pics.ppx"/>
</Parent>
<Parent text="Usage">
<child company="B" text="Tilbud pane" type="video" file="b1.mp4"/>
<child company="B" text="Report pane" type="pdf_document" file="b2.pdf"/>
<child company="N" text="Tilbud pane" type="video" file="n1.mp4"/>
<child company="N" text="Report pane" type="pdf_document" file="n2.pdf"/>
<child company="D" text="Tilbud pane" type="video" file="d1.mp4"/>
<child company="D" text="Report pane" type="pdf_document" file="d2.pdf"/>
</Parent>
</TreeView>
到目前为止,这个 XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8"/>
<xsl:template match="/">
<ul id="LinkedList1" class="LinkedList">
<xsl:apply-templates/>
</ul>
</xsl:template>
<xsl:template match="Parent">
<li>
<xsl:value-of select="@text"/>
<br/>
<ul>
<xsl:apply-templates select="child"/>
</ul>
</li>
</xsl:template>
<xsl:template match="child[@type='video']">
<li>
<a href="{@file}" class="video">
<img src="play_icon.png" alt="video" title="Video tutorial"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</a>
</li>
</xsl:template>
<xsl:template match="child[@type='pdf_document']">
<li>
<a href="{@file}" class="pdfdoc">
<img src="pdf_icon.png" alt="pdfdoc" title="PDF Document"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</a>
</li>
</xsl:template>
<xsl:template match="child[@type='presentation']">
<li><a href="{@file}" class="presentation">
<img src="powerpoint_icon.png" alt="presentation" title="Power Point presentation"/>
<xsl:text> </xsl:text>
<xsl:value-of select="@text"/>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
正如预期的那样,它运行良好,但我想在转换中再添加一项功能。根据 company 属性的值,我希望包含整个元素,或者跳过它。
详细说明:company 属性值为“all”的子元素必须始终包含在转换后的文件中。之后,其余的子元素只有在它们具有,即 company 属性值“B”时才应该被分组。然后我将为不同的公司提供 3 个不同的 XSL 文件。所以我现在只需要一家公司的 XSL 代码。
我不确定是否必须为此使用某种条件语句或模板。我只是被窃听了,因为我的 XSL 文件对我来说有点复杂。
如果有人可以根据我的要求对现有代码进行添加 - 将不胜感激。
【问题讨论】:
-
您好 Milknocookiez,您可以将 XPATH 添加到您的
以过滤 元素: 。这只会给你想要的子元素。但是它们应该如何分组呢?你有没有调查过?最好的问候,彼得 -
@Peter - 谢谢。它们应该以与以前相同的方式进行分组。我需要的唯一补充是区分 company 属性。因此,如果属性是 all 或 B - 它们会被分组,其余的会被跳过。