[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: How to copy only the really existing <choice> bra

Subject: Re: How to copy only the really existing <choice> branch/element? "value-of-if-exist"
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 07 Oct 2009 08:19:08 -0400
Re:  How to copy only the really existing <choice>  bra
At 2009-10-07 14:09 +0200, 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.

Fine ... you have an input element of:


  <top>
    <bbb>myvalue</bbb>
  </top>

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

<xsl:template match="top"> <xsl:copy-of select="*"/> </xsl:template>

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.

Then your processor is broken, because all you are asking for is the string value of each child, and two of the children do not exist.


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>

That surprises me and <xsl:value-of> does not create an element node.


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"/>

But "value-of" is adding text to the result tree, not any elements.


<xsl:copy-of select="*"/> will copy all children of the current node.

<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?

Please demonstrate using an actual stylesheet fragment the behaviour you are citing, because it is not at all expected given the instructions you are using. Oh, and please let us know which processor you are running.


I hope this helps.

. . . . . . . . Ken



--
Upcoming: hands-on code list, UBL, XSLT, XQuery and XSL-FO classes
in Copenhagen Denmark and Washington DC USA, October/November 2009
Interested in other classes?  http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.