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 a-e are all known and wanted. but there is a possibility that other stuff is in there as well which doesnt match the norm. (i.e. 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 a-e 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