Re: [xsl] Adding elements to unknown tree structure

Subject: Re: [xsl] Adding elements to unknown tree structure
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Wed, 21 Sep 2005 23:38:43 +0530
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