簡體   English   中英

如何使用XSLT移動特定的XML元素

[英]How to move Specific XML element using XSLT

我正在嘗試使用XSL文件在特定位置移動XML屬性的特定元素。

假設我有XML

<all>
    <one>Something 1</one>
    <check>
        <present>
            <target> Hello </target>
            <abc> ABC </abc>
        </present>
        <absent> 
            <target> Hi </target>
            <pqr> PQR </pqr>
        </absent>
    </check>
    <action>
        <perform>
            <one>One</one>
            <two>Two</two>
        </perform>
    </action>
</all>

我想要這樣的輸出:

<all>
    <one>Something 1</one>
    <check>
        <present>
                     <abc> ABC </abc>
        </present>
        <absent> 
            <target> Hi </target>
            <pqr> PQR </pqr>
        </absent>
    </check>
    <action>
        <perform>
            <one>One</one>
            <two>Two</two>
            <target> Hello </target>
        </perform>
    </action>
</all>

為此,我寫

       <xsl:template match= "@*|node()" >
    <xsl:copy>
        <xsl:apply-templates select= "@*|node()" />
    </xsl:copy>
</xsl:template >
 <xsl:template match= "action" >
        <xsl:copy>
            <xsl:apply-templates select= "@*|node()" />
            <xsl:apply-templates select= "../check/present/target" mode= "move" />
        </xsl:copy>
    </xsl:template >
<xsl:template match="target" mode="move">
     <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
</xsl:template>

<target>Hello</target>下移動<action>但我希望它里面移動<perform>其是內<action>

如果要將target節點置於perform之下,只需更改模板匹配以匹配perform而不是action

<xsl:template match="action/perform">

甚至是這樣(如果perform只能在action

<xsl:template match="perform">

另外,由於您實際上不是在移動節點,而是添加一個新節點,然后刪除一個現有節點,因此您還需要一個模板來忽略當前target節點

<xsl:template match="present/target" />

試試這個XSLT。 請注意,我如何使身份模板成為命名模板,以避免某些代碼重復。

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

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

    <xsl:template match="present/target" />

    <xsl:template match="action/perform">
        <xsl:copy>
            <xsl:apply-templates select= "@*|node()" />
            <xsl:apply-templates select= "../../check/present/target" mode="move" />
        </xsl:copy>
    </xsl:template >

    <xsl:template match="target" mode="move">
        <xsl:call-template name="identity" />
    </xsl:template>
</xsl:stylesheet>

當然,如果您不打算修改target元素,則可以使用xsl:copy-of

也嘗試一下

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

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

    <xsl:template match="present/target" />

    <xsl:template match="perform">
        <xsl:copy>
            <xsl:apply-templates select= "@*|node()" />
            <xsl:copy-of select= "../../check/present/target" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM