Re: [xsl] Adding elements to unknown tree structure

Subject: Re: [xsl] Adding elements to unknown tree structure
From: Gustave Stresen-Reuter <tedmasterweb@xxxxxxx>
Date: Wed, 21 Sep 2005 19:15:55 +0100
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.


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=""; version="1.0">

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

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

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

<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:when test="$temp = $path2folder">
       <folder name="{@name}">
         <xsl:apply-templates />
         <folder name="{$folder_name}" />
       <folder name="{@name}">
         <xsl:apply-templates />

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

     <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:value-of select="$path" />


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


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

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

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

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

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

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