【问题标题】:XSL: adding a new element to allXSL:向所有人添加新元素
【发布时间】:2015-02-10 08:54:36
【问题描述】:

我是 XSL/XSLT 的新手。我正在尝试向此 xml 的所有用户元素添加一个新元素(数据源):

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
    <user id="1">
        <repo>r1</repo>
        <home>h1</home>
    </user>
    <user id="2">
        <repo>r2</repo>
        <home>h2</home>
    </user>
    <user id="3">
        <repo>r3</repo>
        <home>h3</home>
    </user>
    <user id="4">
        <repo>r4</repo>
        <home>h4</home>
    </user>
    <user id="5">
        <repo>r5</repo>
        <home>h5</home>
    </user>
</users>

我正在使用这个 XSL 脚本:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" cdata-section-elements="configXml"/>
    <!-- Copy everything -->
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/users/user[*]">
        <xsl:copy>
            <xsl:apply-templates/>
            <xsl:element name="datasources"></xsl:element>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

问题是最终结果中所有用户的id都消失了

<users>
<user>
    <repo>r1</repo>
    <home>h1</home>
    <datasources/>
</user>
<user>
    <repo>r2</repo>
    <home>h2</home>
    <datasources/>
</user>
    <user>
    <repo>r3</repo>
    <home>h3</home>
    <datasources/>
</user>
    <user>
    <repo>r4</repo>
    <home>h4</home>
    <datasources/>
</user>
    <user>
    <repo>r5</repo>
    <home>h5</home>
    <datasources/>
</user>

如何在输出中保留用户 ID?

【问题讨论】:

  • 仅供参考 &lt;xsl:element name="datasources"&gt;&lt;/xsl:element&gt;&lt;datasources /&gt; 相同。您无需使用&lt;xsl:element&gt; 来创建具有固定名称的元素。

标签: xml xslt xslt-1.0 xslt-2.0


【解决方案1】:

您需要复制所有模板:

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

How to copy everything as is and only remove a specific element

发生的情况是您的第二个模板与 &lt;user&gt; 元素匹配,但 &lt;apply-templates/&gt; 不会在属性上使用第一个模板,因为它们不匹配 node()

正如 Ian Roberts 指出的那样,您还需要在第二个模板的 &lt;xsl:apply-templates/&gt; 中明确选择 @*|node() 以便也处理属性 - 在这种情况下,它们将被修改后的第一个模板拾取.

所以完整的解决方案是:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" cdata-section-elements="configXml"/>
    <!-- Copy everything -->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/users/user[*]">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <xsl:element name="datasources"></xsl:element>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

【讨论】:

  • &lt;xsl:apply-templates/&gt; 没有select 等价于&lt;xsl:apply-templates select="node()"/&gt;,它包含属性。如果您需要属性以及子节点,则需要专门选择 @*|node()
【解决方案2】:

属性没有被复制,因为你没有复制它们——按照其他模板的模式,在你的/users/user[*]模板中你需要添加一个

<xsl:copy-of select="@*"/>

作为&lt;xsl:copy&gt; 中的第一件事,在将模板应用于子项之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 2018-12-10
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    相关资源
    最近更新 更多