[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: Sun, 25 Sep 2005 16:11:59 +0100
gustave tree
I saw this elsewhere and think it can work. The problem is, the source is actually a tree fragment, so the question I have is how I would modify the sample shown below so that the keys are generated for the tree fragment (if that is even possible).

The source is generated by the following:

<xsl:variable name="all_nodes" select="/root/folder | $path2pageAsSource" />

which gives me the source shown in a prior email. Is it then possible to apply this technique (since the result is a tree fragment)? If not, I'll have to keep looking around for a solution.

BTW, thanks again to everyone who has taken the time to try offering a solution. I never realized how hard it was to "merge" two node sets!

Ted Stresen-Reuter

On Sep 25, 2005, at 3:53 PM, Mukul Gandhi wrote:

This looks like a grouping problem to me. You may use Muenchian
grouping to solve this as shown below.

<?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:key name="by-folder-name" match="folder" use="@name" />

<xsl:template match="/root">
     <xsl:for-each select="folder[generate-id() =
generate-id(key('by-folder-name', @name)[1])]">
       <folder name="{@name}">
         <xsl:copy-of select="key('by-folder-name', @name)/*" />


The XML file is
  <folder name="documents">
      <folder name="personal" />
  <folder name="documents">
    <folder name="public" />

The output produced is
<?xml version="1.0" encoding="UTF-8"?>
   <folder name="documents">
      <folder name="personal"/>
      <folder name="public"/>

This solution is not generic. But the concept might help.


On 9/25/05, Gustave Stresen-Reuter <tedmasterweb@xxxxxxx> wrote:
Thanks for the pointers. I'm getting there, but have an interim


<folder name="documents">
    <folder name="personal" />
<folder name="documents">
    <folder name="public">

How do I get:

<folder name="documents">
    <folder name="personal" />
    <folder name="public">

The few places I've looked indicate that I should use something like

<xsl:copy-of select="not(@name = following::*/@name)" />

but that isn't working. Other things I've tried return the whole
document. Also, I'm afraid that this solution would not return the
name="public" child element of the second folder element.

Basically, I'm looking for a "merged" version of these two fragments.
By "merged" I mean that if an element has the same ancestors, the same
attributes, and the attribute have the same values, but the node
content is different, the content should be added to the same node
appearing earlier in the document.

Any ideas?

Ted Stresen-Reuter

On Sep 21, 2005, at 9:06 PM, Joris Gillis wrote:


Tempore 17:41:53, die 09/21/2005 AD, hinc in
xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit Gustave Stresen-Reuter


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

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: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"
<xsl:variable name="nextpath"
<xsl:variable name="folder" select="folder[@name=$nextpath][$good]"/>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()">
<xsl:with-param name="path2folder" select="$pathafter"/>
<xsl:with-param name="good" select="$verygood"/>
<xsl:if test="$verygood and not($folder) and not($path2folder='')">
<xsl:call-template name="createFolder">
<xsl:with-param name="path2folder" select="$pathafter"/>

<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"
<xsl:if test="not(contains($path2folder,'/'))">
<xsl:comment>folder inserted</xsl:comment>


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

Current Thread


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.
First Name
Last Name
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.