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

Re: Adding elements to unknown tree structure

Subject: Re: Adding elements to unknown tree structure
From: Gustave Stresen-Reuter <tedmasterweb@xxxxxxx>
Date: Wed, 21 Sep 2005 19:15:55 +0100
xsl tree structure
Thanks for the reply. I'm actually working on the "create if doesn't exist" requirement as this is really the most necessary part!

I'll post whatever I come up with for you (and others) to review should you find a use for this.

Ted

On Sep 21, 2005, at 7:08 PM, Mukul Gandhi wrote:

Please try this stylesheet (tested with Saxon 8.5.1)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


<xsl:output method="xml" indent="yes" />

<xsl:param name="folder_name" />
<xsl:param name="path2folder" />

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

<xsl:template match="folder">
   <xsl:variable name="temp">
     <xsl:call-template name="p2f">
       <xsl:with-param name="node" select="." />
       <xsl:with-param name="path" select="@name" />
     </xsl:call-template>
   </xsl:variable>
   <xsl:choose>
     <xsl:when test="$temp = $path2folder">
       <folder name="{@name}">
         <xsl:apply-templates />
         <folder name="{$folder_name}" />
       </folder>
     </xsl:when>
     <xsl:otherwise>
       <folder name="{@name}">
         <xsl:apply-templates />
       </folder>
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template name="p2f">
   <xsl:param name="node" />
   <xsl:param name="path" />

   <xsl:choose>
     <xsl:when test="$node/parent::folder">
       <xsl:call-template name="p2f">
         <xsl:with-param name="node" select="$node/parent::folder" />
         <xsl:with-param name="path"
select="concat($node/parent::folder/@name,'/',$path)" />
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="$path" />
     </xsl:otherwise>
   </xsl:choose>
</xsl:template>

</xsl:stylesheet>

In this stylesheet I have not implemented your requirement
need to make sure that both "root" and "pictures" exist and if they
don't, create them first.

If you really need this to be done, I can try that. But it may take some time..

Regards,
Mukul


On 9/21/05, Gustave Stresen-Reuter <tedmasterweb@xxxxxxx> wrote:
Given:

<myxmlfile>
    <folder name="root">
        <folder name="documents">
            <document name="passwords">
                123456
            </document>
        </folder>
        <folder name="pictures">
            <folder name="family" />
        </folder>
    </folder>
</myxmlfile>

parameter: folder_name = "friends"
parameter: path2folder = root/pictures

How can I create a new folder element named "friends" in the pictures
element?

Keep in mind that I need to make sure that both "root" and "pictures"
exist and if they don't, create them first.

Also, I use a "tokenizer" to split the path in the path2folder
parameter to access the individual values, but it sure would be great
if I could use something like an eval() function to go right to the
folder...

I'm sure I can figure this out myself, but thought I'd check with the
list before losing too much time reinventing the wheel.

Thanks in advance.

Ted Stresen-Reuter

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-2013 All Rights Reserved.