[xsl] How to copy only the really existing <choice> branch/element? "value-of-if-exist"

Subject: [xsl] How to copy only the really existing <choice> branch/element? "value-of-if-exist"
From: "Ben Stover" <bxstover@xxxxxxxxxxx>
Date: Wed, 07 Oct 2009 14:09:59 +0200
Assume I have a XML source based on the following (simplified) XSD schema:

<xsd:element name="top" type="mytype"/>

<xsd:complexType name="mytype">
    <xsd:element name="aaa" type=".....">
    <xsd:element name="bbb" type=".....">
    <xsd:element name="ccc" type=".....">

That mean either element aaa or element bbb or element ccc is filled/exists.

Now I want to copy only this sub-element to a target which currently exists.

If I simply write:

<xsl:value-of select=".../aaa"/>
<xsl:value-of select=".../bbb"/>
<xsl:value-of select=".../ccc"/>

then ALL three elements were copied. The 2 branches which do not exist were created
as empty elements so that the target XML doc looks like e.g.


which is an invalid XML because of its XSD schema file definition.

The question is now: How can I let XSL copy only this branch/element which really exists?
I need a function like

<xsl:value-of-only-the-existing-choice-branch select=..../top"/>


<xsl:value-of-if-exists select=".../aaa"/>
<xsl:value-of-if-exists select=".../bbb"/>
<xsl:value-of-if-exists select=".../ccc"/>

How can I do this in detail?

Thank you

Current Thread