简体   繁体   English

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

[英]Move tag to child element xml - xslt

I have the following XML: 我有以下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. 我想将<prop type="x-idiom-segment-trans-status">*</prop>标记移至所有<tuv>元素。 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) 我只是设法解决了这个问题,但是它复制了所有的prop标签(我不希望<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>

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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