【问题标题】:Conditional XSLT transformation条件 XSLT 转换
【发布时间】: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 - 它们会被分组,其余的会被跳过。

标签: xml xslt


【解决方案1】:

所以如果我使用这个源 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>

考虑样式表中的&lt;xsl:output indent="yes" omit-xml-declaration="yes"/&gt;。在您的原始样式表中,您有 output=html 这会导致您的 &lt;br/&gt; 元素“松开”结束标记。

【讨论】:

  • 它就像魔术一样工作!谢谢。我只是不明白&lt;br /&gt; 失去结束标记的部分。当他们“松开”它时会发生什么?
  • 根据 XHTML 标准,每个标签都需要一个结束标签或一个自结束标签。为了安全起见,我总是确保 br 元素有一个自闭合标签。最好的问候,彼得
猜你喜欢
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2021-12-14
  • 2019-01-23
  • 2015-10-14
相关资源
最近更新 更多