xsl-list
[Top] [All Lists]

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

2009-10-07 08:19:31
Ben Stover wrote:
Assume I have a XML source based on the following (simplified) XSD schema:

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

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

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.

<top>
<aaa></aaa>
<bbb>myvalue</bbb>
<ccc></ccc>
</top>

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

xsl:value-of never creates an elements at all thus I don't see how <xsl:value-of select="ccc"/> would create any element at all in the output.


So somehow I am not sure which problem you face, if there is e.g. no 'ccc' element in the input then neither
  <xsl:value-of select="ccc"/>
nor
  <xsl:copy-of select="ccc"/>
will output any element.



--

        Martin Honnen
        http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--