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 August 12th, 2008, 08:02 AM
Registered User
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Inserting/Creating element in order

I need to be able to create and insert an element by adding it to a particular node in my document. This new element has to be placed in at a particular place around other elements, but all of those elements being completely optional. I won't know wether the other elements are there but I need some way to insert my new one according to the set order.

For Example:

<Sample>
    <Ball/>
    <Bat/>
    <Glove/>
    <Field/>
</Sample>

Now I want to add.. say <Game/> between <Bat/> and <Glove/>. That's where its position should be.

Now say all I have is:

<Sample>
    <Ball/>
</Sample>

My new <Game/> element would have to be placed after <Ball/>
or if I have

<Sample>
    <Ball/>
    <Field/>
</Sample>

My new <Game/> element would be before field but after ball.

I'm kind of at a loss on how to proceed with writing an XSLT to insert this element in the correct position. I can't do it by adding any type of order attributes. Please Help
 
Old August 12th, 2008, 08:18 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

When you are talking about XSLT you don't tend to think in this way, i.e. you don't usually describe your requirements as 'inserting'.

I'm assuming you have some basic input XML, which matches what you have here. Any kind of copy like this usually starts with the Identity Transform:

<xsl:template match="node() | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>


Then you process the ones that have exceptions, something like this:

<xsl:template match="Sample">
    <xsl:copy>
        <xsl:apply-templates select="Ball|Bat"/>
        <Game/>
        <xsl:apply-templates select="Glove|Field"/>
    </xsl:copy>
</xsl:template>

Whether this is what you actually mean or not I'm not 100% sure.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 12th, 2008, 08:20 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:template match="Sample">
  <xsl:copy>
    <xsl:apply-templates select="Ball | Bat"/>
    <Game/>
    <xsl:apply-templates select="Glove | Field"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="Ball | Bat | Glove | Field">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

--
  Martin Honnen
  Microsoft MVP - XML
 
Old August 14th, 2008, 12:54 PM
Registered User
 
Join Date: Aug 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yup that worked out just fine! Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a php order form Beckie Beginning PHP 10 February 15th, 2007 12:20 PM
Inserting a heading only in only the first element stekker XSLT 2 September 28th, 2006 08:06 AM
inserting element wihout template match gonzalocordero XSLT 2 July 3rd, 2006 12:46 PM
creating a table and inserting rows. sasidhar79 Classic ASP Databases 2 July 28th, 2004 03:09 AM
Inserting one element into another srini XSLT 3 January 21st, 2004 08:09 AM





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