简体   繁体   中英

XSLT1.0 find unknown elements in XML and put them elsewhere

Im not sure if the headline is correct. I have a XML-file looking like this:

XML-FILE

<main>
    <mainelement attribute="on">
        <basic>23</basic>
        random
        <stuff id="10"/>
        <a>sometext</a>
        <b_1>1</b_1>
        <b_2>0.300000</b_2>
        <d att="one"/>
        <e>value</e>
    </mainelement>
    <otherlement>
    ...
    </otherlement>
</main>

I format the file to fit a certain norm. Tags ae are all known and wanted. but there is a possibility that other stuff is in there as well which doesnt match the norm. (ie basic, random and stuff). I wrote the following

XSL-Script

<xsl:template match="main/mainelement">
        <mainelement>
            <xsl:copy-of select="a">
            <xsl:variable name="b1" select="b_1"/>
            <xsl:variable name="b2" select="b_2"/>
            <b item1="{b1}" item2="{b2}">
            <xsl:variable name="c" select="c"/>
            <xsl:if test="$c">
                <xsl:copy-of select="c">
            </xsl:if>
            <xsl:if test="not(c)">
                <c>default</c>
            </xsl:if>
            <xsl:copy-of select="d">
            <xsl:variable name="e" select="e"/>
            <xsl:element name="e">
                <xsl:attribute name="att">
                    <xsl:value-of select="$e">
                </xsl:attribute>
            </xsl:element>
        </mainelement>

        <!-- surrounds every element or text() that is unknown with the undefined-tag -->
        <xsl:for-each select="* | text()">
            <xsl:choose>
                <xsl:when test="name()='a'"/>
                <xsl:when test="name()='b_1'"/>
                <xsl:when test="name()='b_2'"/>
                <xsl:when test="name()='c'"/>
                <xsl:when test="name()='d'"/>
                <xsl:otherwise>
                    <undefined>
                        <xsl:copy-of select="current()"/>
                    </undefined>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

that does almost what i want to achieve. The problem is, I dont know if there is anything in there beside the norm, and if nothing like basic, random or stuff is in the above, there should be no < undefinded > tag at all. the output is

OUTPUT

<main>
    <mainelement attribute="on">
        <a>sometext</a>
        <b item1="1" item2="0.3">
        <c>default</c>
        <d att="one"/>
        <e att="value"/>
    </mainelement>
    <undefined>
        <basic>23</basic>
    </undefined>
    <undefined>
        random
    </undefined>
    <undefined>
        <stuff id="10"/>
    </undefined>
    <otherlement>
    ...
    </otherlement>
</main>

but i want to achieve this:

Desired-Output

<main>
    <mainelement attribute="on">
        <a>sometext</a>
        <b item1="1" item2="0.3">
        <c>default</c>
        <d att="one"/>
        <e att="value"/>
    </mainelement>
    <undefined>
        <basic>23</basic>
        random
        <stuff id="10"/>
    </undefined>
    <otherlement>
    ...
    </otherlement>
</main>

i guess i overthought that too much, and so i got a bit blided for an easy solution, so every complete different atempt is apreciated too. A better solution for the xsl:choose method would be cool too, its style creeps me off... the important part is, that i want to exclude everything that doesnt match the norm, and put it somewhere else. But if there is nothing beside the norm, nothing should happen, except my altering of ae I thought of concating variables in the xsl:choose and only create an output if the whole variable was not empty, but that seemed not to be possible in XSL1.0

hope i explained my problem properly. Lots of thanks in advance!

reineke

I am mostly guessing here, but perhaps this is what you're looking for:

XSLT 1.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="/main">
    <xsl:copy>
        <xsl:apply-templates select="mainelement"/>
        <undefined>
            <xsl:copy-of select="mainelement/node()[not(self::a or self::b_1 or self::b_2 or self::c or self::d or self::e)]"/>
        </undefined>
        <xsl:copy-of select="otherlement"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="mainelement">
    <xsl:copy>
        <xsl:copy-of select="@* | a"/>
        <b item1="{b_1}" item2="{b_2}"/>
        <xsl:copy-of select="c"/>
        <xsl:if test="not(c)">
            <c>default</c>
        </xsl:if>
        <xsl:copy-of select="d"/>
        <e att="{e}"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Applied to your example input, the result is:

<?xml version="1.0" encoding="UTF-8"?>
<main>
   <mainelement attribute="on">
      <a>sometext</a>
      <b item1="1" item2="0.300000"/>
      <c>default</c>
      <d att="one"/>
      <e att="value"/>
   </mainelement>
   <undefined>
      <basic>23</basic>
        random
        <stuff id="10"/>
   </undefined>
   <otherlement>
    ...
    </otherlement>
</main>

Alternatively, you could do a slightly more elegant:

<xsl:template match="/main">
    <xsl:copy>
        <xsl:apply-templates select="mainelement"/>

        <xsl:variable name="whitelist" select="mainelement/a | mainelement/b_1 | mainelement/b_2 | mainelement/c | mainelement/d | mainelement/e " />
        <undefined>
            <xsl:copy-of select="mainelement/node()[not(count(.|$whitelist) = count($whitelist))]"/>
        </undefined>

        <xsl:copy-of select="otherlement"/>
    </xsl:copy>
</xsl:template>

or, if your processor supports the EXSLT set:difference() extension function:

<xsl:template match="/main">
    <xsl:copy>
        <xsl:apply-templates select="mainelement"/>

        <xsl:variable name="whitelist" select="mainelement/a | mainelement/b_1 | mainelement/b_2 | mainelement/c | mainelement/d | mainelement/e " />
        <undefined>
            <xsl:copy-of select="set:difference(mainelement/node(), $whitelist)"/>
        </undefined>

        <xsl:copy-of select="otherlement"/>
    </xsl:copy>
</xsl:template>

Edit:

it almost does what i want, except the following: If there is noting undefined (ie no basic, random, stuff) in the mainelement, there should be no "undefined" tag at all.

Well, then put the "undefined" nodes in a variable first, then create the <undefined> element only if there's something in that variable:

<xsl:variable name="undefined" select="mainelement/node()[not(self::a or self::b_1 or self::b_2 or self::c or self::d or self::e)]"/>
<xsl:if test="$undefined">
    <undefined>
        <xsl:copy-of select="$undefined"/>
    </undefined>
</xsl:if>

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