简体   繁体   中英

Edit an xml tag based on the value of its attribute with xslt file

I wish I could edit an xml file with an XLS file to keep only tags who have the attribute "name" = LEVEL1

My XML file INPUT :

<text_test name="Main">
    <text name="LEVEL1">
        <p name="AS">You want me</p>
        <p name="AS">I'm Batman</p>
    </text>
    <text name="LEVEL1">
        <p name="AS">You want me too</p>
        <p name="AS">I'm Superman</p>
    </text>
    <text name="NOPE">
        <p name="AS">I don't want you</p>
        <p name="AS">You're Bruce Wayne</p>
    </text>
    <text name="TOTO">
        <p name="AS">I don't want you too</p>
        <p name="AS">You're Bob the Sponge</p>
    </text>
</text_test>

The xml file OUPUT whoI would like to have :

<Main>
    <LEVEL1>
        <AS>You want me</AS>
        <AS>I'm Batman</AS>
    </LEVEL1>
    <LEVEL1>
        <AS>You want me too</AS>
        <AS>I'm Superman</AS>
    </LEVEL1>
</Main>

My XSLT file :

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

    <xsl:template match="text_test">
        <xsl:param name="text_test"/>
            <xsl:element name="{@name}">
                <xsl:for-each select="child::text">
                    <xsl:element name="{@name}">
                        <xsl:for-each select="child::p">
                            <xsl:element name="{@name}">
                                <xsl:value-of select= "node()"/>
                            </xsl:element>
                        </xsl:for-each>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>
    </xsl:template>

</xsl:stylesheet>

What I have in OUTPUT actually :

<Main>
    <LEVEL1>
        <AS>You want me</AS>
        <AS>I'm Batman</AS>
    </LEVEL1>
    <LEVEL1>
        <AS>You want me too</AS>
        <AS>I'm Superman</AS>
    </LEVEL1>
    <NOPE>
        <AS>I don't want you</AS>
        <AS>You're Bruce Wayne</AS>
    </NOPE>
    <TOTO>
        <AS>I don't want you too</AS>
        <AS>You're Bob the Sponge</AS>
    </TOTO>
</Main>

I have trying to make this with a variable whose content is the value of the attribute "name" and after to use a "when" but i'm fail too...

You should switch to a template based approach, as this will simplify the logic greatly

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

    <xsl:strip-space elements="*" />

    <xsl:template match="*">
        <xsl:element name="{@name}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

    <xsl:template match="text[@name != 'LEVEL1']" />
</xsl:stylesheet>

So, the first template is a generic template which will create a new element based on the name attribute (This assumes all your elements have such an attribute).

The second template, will match text elements which don't have a name of "LEVEL1" and ignore them. Do note this second template will take priority over the first due to how it explicitly matches a named element (and has a condition).

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