Xml 条件XSLT转换
我有这个XML:Xml 条件XSLT转换,xml,xslt,Xml,Xslt,我有这个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="
<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属性的值,我希望包含整个元素,或者跳过它
精化:其公司属性值为“all”的子元素必须始终包含在转换的文件中。之后,仅当其他子元素具有公司属性值“B”时,才应将其分组。然后,我将为不同的公司提供3个不同的XSL文件。所以我现在只需要一家公司的XSL代码
我不确定我是否必须使用某种条件语句或模板来实现这一点。我只是被窃听了,因为我的XSL文件对我来说有点复杂
如果有人能将我的需求添加到现有代码中,我们将不胜感激。因此,如果我使用此源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>
并应用此XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<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[@company='all' or @company='B']"/>
</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"/>
</img>
</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"/>
</img>
</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"/>
</img>
</a>
</li>
</xsl:template>
</xsl:stylesheet>
您将获得以下输出:
<ul class="LinkedList" id="LinkedList1">
<li>Installation<br/>
<ul>
<li>
<a class="video" href="startup.mp4">
<img title="Video tutorial" alt="video" src="play_icon.png"> Startup</img>
</a>
</li>
<li>
<a class="video" href="small.mp4">
<img title="Video tutorial" alt="video" src="play_icon.png"> Getting there</img>
</a>
</li>
<li>
<a class="pdfdoc" href="test.pdf">
<img title="PDF Document" alt="pdfdoc" src="pdf_icon.png"> Steps</img>
</a>
</li>
<li>
<a class="presentation" href="pics.ppx">
<img title="Power Point presentation" alt="presentation"
src="powerpoint_icon.png"> Pictures</img>
</a>
</li>
</ul>
</li>
<li>Usage<br/>
<ul>
<li>
<a class="video" href="b1.mp4">
<img title="Video tutorial" alt="video" src="play_icon.png"> Tilbud pane</img>
</a>
</li>
<li>
<a class="pdfdoc" href="b2.pdf">
<img title="PDF Document" alt="pdfdoc" src="pdf_icon.png"> Report pane</img>
</a>
</li>
</ul>
</li>
</ul>
- 安装
-
-
-
-
- 用法
-
-
考虑样式表中的
。在原始样式表中有output=html
,这会导致
元素“松开”结束标记
我希望产出如你所期望的那样
致以最良好的祝愿,
PeterHello Milkoncookiez,您可以将XPATH添加到to筛选器元素:。这将只提供所需的子元素。但它们应该如何分组呢?你调查过了吗?顺致敬意,PEter@Peter-谢谢。它们的分组方式应与以前相同。我唯一需要的补充是区分company属性。因此,如果属性是all或B,它们将被分组,其余的将被跳过。这就像魔术一样!谢谢我只是不理解
松开结束标记的部分。当他们“松开”它时会发生什么?根据XHTML标准,每个标记都需要一个结束标记或一个自动结束标记。为了安全起见,我总是确保br元素有一个自动关闭标签。向你问好,彼得