Altova Mailing List Archives

Re: [xsl] document tree fragments

From: "G. Ken Holman" <gkholman@-------------------->
Date: 10/11/2008 12:38:00 PM
At 2008-10-11 21:31 +1300, Joe Barwell wrote:
I have succeeded, but with two problems. Firstly, I am unable to 
separately specify the directory path of the files. Here is my 
"base" xml file's content:

<docs path="jb">
        <doc filename="jb/wine1.xml" />
        <doc filename="jb/wine2.xml" />
        <doc filename="jb/wine3.xml" />

Note that I have provided the directory path within the @filename 
attribute, but I would prefer to retrieve it from the @path attribute.

Is it true that you want *only* the filename of @filename to be added 
to @path, or do you want that portion of @filename that matches @path 
to be removed and if there are more directory steps they are to be included?

In XSLT 1.0 you will have to use extension functions to work with 
manipulated versions of the attributes.  If you gave up on the @path 
attribute and only used @filename, then you don't need to resort to 
extensions and you can use:


... to get a node set of the document nodes of all of the files.  If 
you have to do any manipulation on each filename then you have to 
resort to using extension functions.

<xsl:template match="/docs">

        <xsl:variable name="thePath" select="@path" />

        <xsl:variable name="theWines" 
select="document(doc/@filename)/wine" />


Is there a way to use my $thePath variable, instead of including the 
directory path inside the @filename attribute? I tried to 
concatenate, but without success.

No, there is no way without creating a secondary structure with the 
values you want and then using an extension in XSLT 1.0 to read that 
structure as a node set.

I want to deal with all my xml files together, rather than using a 
for-each loop, because of several aggregate processes that I want to apply.

Yes, your algorithms will work better when you deal with them all 
together as a node set of root nodes or document elements.

Secondly, I'm not sure I fully understand what my $theWines variable 
now contains. I believe it is a collection of tree fragments--i.e. 
there's no single top-most node, is that right? Or is there an 
implicit / (document) node?

No ... if you open five files you end up with a node set of five nodes.

The reason I ask is because I have some templates that rely on 
processing the preceding axis, and whereas my templates work fine 
for my < wines > xml file, they do not produce the anticipated 
results when using my $theWines variable.

Correct ... trees are independent of each other.  The processor keeps 
an implicit order (of its own determination) *between* trees, but 
there are no axes that span across trees.

You need to do grouping cross multiple trees which is done using the 
variable-based method.

Since you haven't posted examples, I'll sketch out what you need 
without testing it:

  <xsl:variable name="countries" select="$wines/wine/countries/country"/>
    <xsl:for-each select="$countries">
      <xsl:sort select="name"/>
      <!--create a list when encountering the first of them all-->
      <xsl:if test="generate-id(.)=
        <xsl:variable name="this-country"
            <xsl:value-of select="name"/>
            <xsl:value-of select="count($this-country)"/>

Do I need to wrap the sequence of tree fragments in my $theWines 
variable inside a node?

You could but then you'd have to use an extension (the same node set 
extension) to access the resulting tree ... the above variable method 
works without extensions and across multiple trees in XSLT 1.0.

Chapter 9 of my PDF book and XSLT video goes into the three methods 
of grouping in XSLT 1.0 (axis, key and variable) and when to use each 
(and how XSLT 2.0 is so very superior).

I hope this helps.

. . . . . . . . . . . . Ken

Upcoming XSLT/XSL-FO hands-on courses:      Wellington, NZ 2009-01
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:
Video course overview:
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.
Male Cancer Awareness Nov'07
Legal business disclaimers:


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.