【问题标题】:Add, move, and rearrange XML elements within one XSL configuration file在一个 XSL 配置文件中添加、移动和重新排列 XML 元素
【发布时间】:2016-07-28 20:30:05
【问题描述】:

我有一个要使用 XSLT 修改的 XML 文件。以下更改包括: 1. 将新元素插入特定节点。 2. 将元素移动到不同的层次结构。 3. 以特定顺序重新排列父项下的元素。

我能够完成 1 和 2,但是当应用 3 时,一切都中断了。请查看 XML 输入和我目前拥有的代码。非常感谢任何帮助。

XML 输入

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <a>
        <a.1>1</a.1>
        <a.2>2</a.2>
    </a>
    <b/>
    <c>
        <c.1>1</c.1>
        <c.2>
            <cx.1>1</cx.1>
            <cx.2>
                <cc.1>10</cc.1>
            </cx.2>
            <cx.5>m</cx.5>
        </c.2>
    </c>
    <!-- need to move this node under x -->
    <y>
        <y.1>A</y.1>
        <!-- 2 more elements will be added here -->
        <y.4/>
    </y>
    <d/>
    <g/>
    <f>
        <f.1></f.1>
        <!-- need to move this node at the bottom of root -->
        <z>
            <z.1>11</z.1>
            <!-- 2 more elements will be added here -->
            <z.4/>
        </z>
    </f>
    <x>
        <x.1>1</x.1>
        <x.2>
            <ax.1>1</ax.1>
            <ax.5>y</ax.5>
        </x.2>
    </x>
</root>

期望的输出

<root>
<a>...</a>
    <b/>
    <c>...</c>
    <d/>
    <g/>
    <f>
        <f.1></f.1>
        <!-- node z is moved -->
    </f>
    <x>...</x>
    <!-- move nodes y here -->
    <y>
        <y.1>A</y.1>
        <y.2>B</y.2> <!-- added element -->
        <y.3>3</y.3> <!-- added element -->
        <y.4/>
    </y>
    <!-- move nodes z here from root/f/z -->
    <z>
        <z.1>11</z.1>
        <z.2>22</z.2> <!-- added element -->
        <z.3>33</z.3> <!-- added element -->
        <z.4/> 
    </z>
<root>

这是我写的代码

<?xml version="1.0" encoding="UTF-8"?>

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

<!-- rearrangement -->
<xsl:template match="root">
    <xsl:copy>
        <xsl:apply-templates select="a" />
        <xsl:apply-templates select="b" />
        <xsl:apply-templates select="c" />
        <xsl:apply-templates select="d" />
        <xsl:apply-templates select="g" />
        <xsl:apply-templates select="f" />
        <xsl:apply-templates select="x" />
        <xsl:apply-templates select="y" />
    </xsl:copy>
</xsl:template>

<xsl:template match="y">
    <y>
        <y.1><xsl:value-of select="y.1"/></y.1>
        <y.2>B</y.2> <!-- added element -->
        <y.3>3</y.3> <!-- added element -->
        <y.4/>
    </y>
</xsl:template>

<xsl:template match="z">
    <z>
        <z.1><xsl:value-of select="z.1"/></z.1>
        <z.2>Z</z.2>   <!-- added element -->
        <z.3>33</z.3> <!-- added element -->
        <z.4/>
    </z>
</xsl:template>

<!-- relocate node z to bottom of root -->
<xsl:template match="root">
    <xsl:copy>
            <xsl:apply-templates/>
            <xsl:apply-templates select="/root/f/z"/>
    </xsl:copy>
</xsl:template>

<!-- Start to break from here -->
<!-- remove this specific node -->
<xsl:template match="/root/f/z"/>
</xsl:stylesheet>

【问题讨论】:

  • 是 xslt 的要求,还是你对一些 java 编码没问题?
  • 是的,xslt 是一项要求。我可以使用 2 个或更多 xsl 样式表进行转换,但我的目标是尽可能将其变成一个。

标签: xml xslt


【解决方案1】:

考虑运行&lt;f&gt; 模板匹配,因为&lt;z&gt; 是它的子级。对于&lt;z&gt;,只需缩小到它的位置:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

<!-- rearrangement -->
<xsl:template match="root">
    <xsl:copy>
        <xsl:apply-templates select="a" />
        <xsl:apply-templates select="b" />
        <xsl:apply-templates select="c" />
        <xsl:apply-templates select="d" />
        <xsl:apply-templates select="g" />
        <xsl:apply-templates select="f"/>
        <xsl:apply-templates select="x" />
        <xsl:apply-templates select="y" />        
        <xsl:apply-templates select="f/z"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="f">
    <f>        
        <xsl:apply-templates select="*[not(name()='z')]"/> <!-- all children but z -->
    </f>
</xsl:template>

<xsl:template match="y">
    <y>
        <y.1><xsl:value-of select="y.1"/></y.1>
        <y.2>B</y.2> <!-- added element -->
        <y.3>3</y.3> <!-- added element -->
        <y.4/>
    </y>
</xsl:template>

<xsl:template match="z">
    <z>
        <z.1><xsl:value-of select="z.1"/></z.1>
        <z.2>Z</z.2>   <!-- added element -->
        <z.3>33</z.3> <!-- added element -->
        <z.4/>
    </z>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    相关资源
    最近更新 更多