将标记移动到子元素xml - xslt

时间:2017-07-21 10:53:00

标签: xml xslt

我有以下XML:

<tmx version="1.4">
    <header creationtool="Olifant" creationtoolversion="3.0.8.0" datatype="xml" segtype="sentence" adminlang="en-US" srclang="IT-IT" o-tmf="SDL TM8 Format" creationdate="20170508T152943Z" creationid="Daniela Cosenza" changedate="20170721T095005Z" changeid="d">
    </header>
        <body>
            <tu>
            <prop type="x-idiom-source-ipath">THIS IS A TEST.docx</prop>
            <prop type="x-idiom-segment-trans-status">pending</prop>
                <tuv xml:lang="IT-IT">
                <prop type="x-idiom-segment-status">locked</prop>
                    <seg>Calza donna gambaletto con fantasia pois in lana lurex che termina nel polso.</seg>
            </tuv>
            <tuv xml:lang="EN-GB" creationdate="20151008T135602Z" creationid="Sarah Goldman" changedate="20151009T110927Z" changeid="Sarah Goldman">
                <seg>Women's knee sock with polka-dot pattern in wool lurex that ends at the top.</seg>
            </tuv>
        </tu>
    </body>
</tmx>

我想将<prop type="x-idiom-segment-trans-status">*</prop>标记移动到所有<tuv>元素。这是预期的输出:

<tmx version="1.4">
    <header creationtool="Olifant" creationtoolversion="3.0.8.0" datatype="xml" segtype="sentence" adminlang="en-US" srclang="IT-IT" o-tmf="SDL TM8 Format" creationdate="20170508T152943Z" creationid="Daniela Cosenza" changedate="20170721T095005Z" changeid="d">
    </header>
    <body>
        <tu>
        <prop type="x-idiom-source-ipath">THIS IS A TEST.docx</prop>
            <tuv xml:lang="IT-IT">
            <prop type="x-idiom-segment-status">locked</prop>
            <prop type="x-idiom-segment-trans-status">pending</prop>
                <seg>Calza donna gambaletto con fantasia pois in lana lurex che termina nel polso.</seg>
            </tuv>
            <tuv xml:lang="EN-GB" creationdate="20151008T135602Z" creationid="Sarah Goldman" changedate="20151009T110927Z" changeid="Sarah Goldman">
                <seg>Women's knee sock with polka-dot pattern in wool lurex that ends at the top.</seg>
            <prop type="x-idiom-segment-trans-status">pending</prop>
            </tuv>
        </tu>
    </body>
</tmx>

我只是想出了这个,但它复制了所有的道具标签(我不希望移动<prop type="x-idiom-source-ipath">THIS IS A TEST.docx</prop>元素)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="prop" />
    <xsl:template match="tuv">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:copy-of select="../prop" />
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

提前谢谢!

1 个答案:

答案 0 :(得分:0)

差不多正确。

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

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

    <xsl:template match="prop[@type = 'x-idiom-segment-trans-status']" />

    <xsl:template match="tuv">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:copy-of select="../prop[@type = 'x-idiom-segment-trans-status']" />
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>