Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2008, 01:52 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default Inserting into a tree with optional elements

I'm new to XSLT. I've been learning quite a bit over the last month. I'm trying to create an XSLT that looks at a particular node of the source document and inserts a variable into that node. Basically I'm taking 2 XML documents and generating a new document.

The problem is the node of in interest in the source document has 42 optional elements. I need to insert this variable into a specific location based on the schema of the source document. For example the source document is structured like so:
Code:
<Top>
    <Element>
        <ChildElement>
            <OPTIONAL_1> Data </OPTIONAL_1>
            <OPTIONAL_2> Data </OPTIONAL_2>
            <OPTIONAL_3> Data </OPTIONAL_3>
            ..............................
            ..............................
            <OPTIONAL_42> Data </OPTIONAL_42>
        </ChildElement>
    </Element>
</Top>
The variable that I will be inserting is simply another XML document. It looks something like this:
Code:
<xsl:variable name="VAR">
<XML_Top>
    <XML_Data1> DATA 1 </XML_Data1>
    <XML_Data2> DATA 2 </XML_Data2>
    <XML_Data2> DATA 2 </XML_Data3>
</XML_Top>
</xsl:variable>

This variable needs to be inserted as the 30th optional element in the source document. (Or the correct relative location based on which optional elements are present). So the results should look like this:
Code:
<Top>
    <Element>
        <ChildElement>
            <OPTIONAL_1> Data </OPTIONAL_1>
            <OPTIONAL_2> Data </OPTIONAL_2>
            <OPTIONAL_3> Data </OPTIONAL_3>
            ..............................
            ..............................
            <OPTIONAL_29> Data </OPTIONAL_29>
            <XML_Top>
                <XML_Data1> DATA 1 </XML_Data1>
                <XML_Data2> DATA 2 </XML_Data2>
                <XML_Data2> DATA 2 </XML_Data3>
            </XML_Top>
            <OPTIONAL_31> Data </OPTIONAL_31>
            ..........................
            ..........................
            <OPTIONAL_42> Data </OPTIONAL_42>
        </ChildElement>
    </Element>
</Top>
For any given instance of the source document, there could be any number of those optional elements. Based on the schema, this variable can only be in a specific location in the source document. If all of the elements were required, this is very simple to do. But since all of them are optional, I do not know how to locate where the variable should be inserted. All the other elements/attributes and data in the source document must be maintained in the output. I'm on a windows platform and this will eventually run on a Saxon XSLT 2.0 compatible processor. Any help would be appreciated!


 
Old April 24th, 2008, 03:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This one had me stumped for 30 seconds or so, but in fact it's not difficult:

<xsl:template match="ChildElement">
  <xsl:copy>
    <xsl:copy-of select="OPTIONAL_1, OPTIONAL_2, ....,
                   OPTIONAL_29,
                   $VAR,
                   OPTIONAL_30, OPTIONAL_31, ....,
                   OPTIONAL_42"/>
  </xsl:copy>
</xsl:template>

This hinges on the fact that if one of the child elements doesn't exist, the copy just ignores it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
The Following User Says Thank You to mhkay For This Useful Post:
iceandrews (December 17th, 2008)
 
Old April 24th, 2008, 08:59 AM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

Fantastic. You know sometime with this kind of language, you really over think how complicated the logic should be.

You should've seen the amount of IF statements I had. Perfectly simple solution. Thank you!

(BTW, Love the books)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Optional element exists if has inner elements 2BOrNot2B XML 0 May 9th, 2008 02:11 PM
Optional prameters and C#? NewTitle2007 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 August 5th, 2007 12:17 PM
Argument not Optional aziaraphale Excel VBA 5 August 1st, 2007 02:53 AM
how to control optional elements and attributes NEO1976 XSLT 5 September 4th, 2006 02:58 AM
how to exclude elements in the result tree output ntmt XSLT 0 May 25th, 2006 10:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.