Add, move, and rearrange XML elements within one XSL configuration file

时间:2016-07-28 20:30:05

标签: xml xslt

I have an XML file to be modify using XSLT. The following changes include: 1. Insert new elements to a specific node. 2. Move the element to a different hierarchy. 3. Rearrange elements under parent in some specific order.

I was able to complete 1 and 2 but when 3 applied everything break. Please take a look at the XML input and the code I have so far. Greatly appreciated any help.

XML input

<?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>

Desired output

<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>

Here is what I have coded

<?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>

1 个答案:

答案 0 :(得分:0)

考虑运行<f>模板匹配,因为<z>是其子级。对于<z>,只需缩小到其位置:

<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>