xsl-list
[Top] [All Lists]

Re: Adding elements to unknown tree structure

2005-09-21 13:06:05
Hi,

Tempore 17:41:53, die 09/21/2005 AD, hinc in 
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com scripsit Gustave Stresen-Reuter 
<tedmasterweb(_at_)mac(_dot_)com>:

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.

See if you can get this stylesheet working:
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:output method="xml" indent="yes"/>
        
<xsl:template match="/">
<xsl:apply-templates>
<xsl:with-param name="path2folder">/root/pictures/friends</xsl:with-param>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="node()">
<xsl:param name="path2folder"/>
<xsl:param name="good" select="true()"/>
<xsl:variable name="path" select="substring-before($path2folder,'/')"/>
<xsl:variable name="verygood"
        select="($good and @name=$path) or self::myxmlfile"/>
<xsl:variable name="pathafter" select="substring-after($path2folder,'/')"/>
<xsl:variable name="nextpath" select="substring-before($pathafter,'/')"/>
<xsl:variable name="folder" select="folder[(_at_)name=$nextpath][$good]"/>
<xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="node()">
                <xsl:with-param name="path2folder" select="$pathafter"/>
                <xsl:with-param name="good" select="$verygood"/>
        </xsl:apply-templates>
        <xsl:if test="$verygood and not($folder) and not($path2folder='')">
                <xsl:call-template name="createFolder">
                        <xsl:with-param name="path2folder" select="$pathafter"/>
                </xsl:call-template>
        </xsl:if>
</xsl:copy>
</xsl:template>

<xsl:template name="createFolder">
<xsl:param name="path2folder"/>
<folder name="{substring-before(concat($path2folder,'/'),'/')}">
        <xsl:if test="contains($path2folder,'/')">
                <xsl:call-template name="createFolder">
                        <xsl:with-param name="path2folder"
                                select="substring-after($path2folder,'/')"/>
                </xsl:call-template>
        </xsl:if>
        <xsl:if test="not(contains($path2folder,'/'))">
                <xsl:comment>folder inserted</xsl:comment>
        </xsl:if>
</folder>
</xsl:template>

</xsl:stylesheet>



regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Don't send spam. I don't like it and it is illegal.

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--