简体   繁体   中英

Move tag to child element xml - xslt

I have the following 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>

I'd like to move the <prop type="x-idiom-segment-trans-status">*</prop> tag to all the <tuv> elements. This is the expected output:

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

I only managed to come up with this, but it copies ALL the prop tags (I don't want the <prop type="x-idiom-source-ipath">THIS IS A TEST.docx</prop> element to be moved)

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

Thank you in advance!

Almost correct.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM