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, 28 Sep 2005 16:35:54 +0100
Ok, ok... Included please find two documents:

- add.xsl
- data.xml

add.xsl calls a template (tokenize) that is not included since, I believe, it is copyrighted by either Jeni Tennison or Sal Mangano and I've asked neither of them for permission to "publish" it here (but here's a link to a template that is strikingly similar to the one I'm using: http://www.exslt.org/str/functions/tokenize/ str.tokenize.template.xsl.html that might just do the same exact thing)

use the following command to see the results (the addition of elements as specified by the parameters):

xsltproc --stringparam path2page 'root/mydocuments/pictures/holiday' --stringparam pagename 'mypic.jpg' add.xsl data.xml

The nice thing is that the stylesheet is smart enough to insert the elements as indicated by path2page without overwriting existing content and appends all necessary subfolder.

I'd love to hear of any successes or failures using this stylesheet.

Thanks again to all those that helped.

Ted Stresen-Reuter
http://www.tedmasterweb.com

DATA.XML

<?xml version="1.0" encoding="utf-8"?>
<root>
    <folder name="mydocuments">
        <folder name="pictures">
            <folder name="family">
                <folder name="weddings">
                    <page name="mine.jpg" />
                    <page name="sister.jpg" />
                </folder>
            </folder>
            <folder name="friends">
                <folder name="fiestas">
                    <page name="bachelorette_party.jpg" />
                </folder>
            </folder>
        </folder>
    </folder>
</root>



ADD.XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:exsl="http://exslt.org/common";
extension-element-prefixes="exsl">


<xsl:output method="xml" omit-xml-declaration="no" version="1.0" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*" />


    <xsl:param name="pagename" />
    <xsl:param name="path2page" />

<xsl:variable name="path2pageAsNodes-RTF">
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="$path2page"/>
<xsl:with-param name="delimiters" select="'/'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="path2pageAsNodes" select="exsl:node-set($path2pageAsNodes-RTF)" />


<xsl:variable name="path2pageAsSource-RTF">
<xsl:call-template name="createChildRecursive">
<xsl:with-param name="folder_name" select="$path2pageAsNodes/token[position() = 1]"/>
<xsl:with-param name="child_name" select="$path2pageAsNodes/token[position() = 2]"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="path2pageAsSource" select="exsl:node-set($path2pageAsSource-RTF)" />


<xsl:template name="insert_page">
<xsl:element name="page">
<xsl:attribute name="name"><xsl:value-of select="$pagename" /></xsl:attribute>
</xsl:element>
</xsl:template>


<xsl:template name="createChildRecursive">
<xsl:param name="folder_name" />
<xsl:param name="child_name" />
<xsl:variable name="grandchild" select="$path2pageAsNodes/token[preceding-sibling::* = $child_name]" />
<xsl:element name="folder">
<xsl:attribute name="name"><xsl:value-of select="$folder_name" /></xsl:attribute>
<xsl:choose>
<xsl:when test="$child_name != ''">
<xsl:call-template name="createChildRecursive">
<xsl:with-param name="folder_name" select="$child_name" />
<xsl:with-param name="child_name" select="$grandchild" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="insert_page" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>


    <xsl:template name="path2root">
        <xsl:param name="nodes" />
        <xsl:for-each select="$nodes/ancestor-or-self::folder">
            <xsl:value-of select="@name" />
            <xsl:if test="position() != last()">
                <xsl:text>/</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

<!--
NOTES
- Paths that are the same, path/to/my/doc and path/to/my/docs, may mistakenly be matched
-->


<xsl:template match="folder">

<xsl:variable name="depth" select="count(ancestor-or-self::*) - 1" />
<xsl:variable name="elem" select="@name" />
<xsl:variable name="sister" select="$path2pageAsNodes/token[position() = $depth]" />
<xsl:variable name="target_elem_child" select="$path2pageAsNodes/token[position() = ( $depth + 1 ) ]" />
<xsl:variable name="target_elem_grandchild" select="$path2pageAsNodes/token[position() = ( $depth + 2 ) ]" />
<xsl:variable name="this_elem_child" select="child::folder[@name = $target_elem_child]/@name" />
<xsl:variable name="child_is_a_match" select="count(descendant-or-self::folder[@name = $target_elem_child])" />
<xsl:variable name="path2root">
<xsl:call-template name="path2root">
<xsl:with-param name="nodes" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:variable name="ancestor_path">
<xsl:for-each select="ancestor::folder">
<xsl:value-of select="@name" />
<xsl:if test="position() != last()">
<xsl:text disable-output-escaping="yes">/</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>


<xsl:choose>
<xsl:when test="$elem = $sister">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:choose>
<xsl:when test="$target_elem_child != ''">
<xsl:if test="$child_is_a_match = 0">
<xsl:variable name="is_substring" select="starts-with($path2page, $ancestor_path)" />
<xsl:if test="$is_substring">
<xsl:call-template name="createChildRecursive">
<xsl:with-param name="folder_name" select="$target_elem_child" />
<xsl:with-param name="child_name" select="$target_elem_grandchild" />
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$path2root = $path2page">
<xsl:call-template name="insert_page" />
</xsl:if>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$depth = 1">
<xsl:call-template name="createChildRecursive">
<xsl:with-param name="folder_name" select="$sister" />
<xsl:with-param name="child_name" select="$target_elem_child" />
</xsl:call-template>
</xsl:if>
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>


</xsl:template>



<xsl:template match="/| node() | @* | comment() | processing-instruction()">

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

</xsl:template>


</xsl:stylesheet>



On Sep 27, 2005, at 11:17 PM, JBryant@xxxxxxxxx wrote:


Suppose some poor soul five years from now has the same or a similar
problem and wants to see your stylesheet? And suppose that, during that
time, you've taken early retirement, moved to a chateau on the French
Riviera, and changed e-mail addresses and web sites? Then that person
would be better served to have your stylesheet in the archives. So I think
it would be better to post it. Of course, if you could take the time to
create a generic XML file and an XSL file that expresses the problem and
solution in the smallest possible way, that would be better still.


Just my opinion.

Jay Bryant
Bryant Communication Services
(presently consulting at Synergistic Solution Technologies)




Gustave Stresen-Reuter <tedmasterweb@xxxxxxx> 09/27/2005 05:01 PM Please respond to xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To xsl-list@xxxxxxxxxxxxxxxxxxxxxx cc

Subject
Re: [xsl] Adding elements to unknown tree structure






Ok, I finally got a stylesheet that works. Thanks to everyone that helped. This stylesheet allows you to pass two parameters and inserts all the necessary elements exactly where needed without knowing the content of the XML beforehand.

For example, if I want to insert a document in
root/mydocs/pictures/holidays/amsterdam I would send path2doc as
"root/mydocs/pictures/holidays/amsterdam" and the stylesheet would take
care of creating any missing folder elements and would NOT overwrite
content already there.

I would like to share this stylesheet with whoever wants it but it is a
little long (over 200 lines) and requires a utility template (tokenize,
another 200+ lines) so I don't think publishing it on this list would
be a good idea.

Therefore, anyone wanting to see this stylesheet can contact me off
line and I will send you the files. If there is enough interest (highly
doubt it), I will post it on my web site.

Thanks again.

Ted Stresen-Reuter
http://www.tedmasterweb.com

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">
   <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)/*" />
       </folder>
     </xsl:for-each>
   </root>
</xsl:template>

</xsl:stylesheet>

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

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

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

Regards,
Mukul

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

Given

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

How do I get:

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

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

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

Hi,

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

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[@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@xxxxxxxxxxxxxxxxxxxxxx>
--~--

Current Thread