Re: [xsl] ancestor question

Subject: Re: [xsl] ancestor question
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 28 Sep 2001 18:55:08 -0400
Amy,

If I construe this correctly, you are falling into a subtle trap that often springs up to confound newer users.

The trick is, that the value of a node-set is the string value of the *first* node in the node-set (in document order).

So when your context node is an Aps:Drawing, and you ask for <xsl:value-of
select="ancestor::Aps:VpmResultSet/Aps:PartStructure/Aps:Name"/>, the XPath traverses all the way up to the ancestor VpmResultSet (actually all of them, but you only have one), then down to collect all the Aps:Name children of its Aps:PartStructure children. It then has a bunch of nodes collected. But since you ask for the value, you get the value only of the first one. That's why the Name that comes out is always the same ("assembly 1").


To identify correctly the Name that belongs to each Aps:Drawing, you need to traverse in a way that will avoid this pitfall. It looks from your data like going up just to ancestor::PartStructure (and no further), and then down, would work.

As background, it may help you to know that the monster expression

ancestor::Aps:VpmResultSet/Aps:PartStructure/Aps:Name

is actually short for

ancestor::Aps:VpmResultSet/child::Aps:PartStructure/child::Aps:Name

(Seeing it explicit like this helps to dramatize how the steps work in the location path. Given the XPath, you get back *all* the Aps:Name children of *all* the Aps:PartStructure children of *all* the ancestor Aps:VpmResultSet nodes of your context node. This may be many Names. When you ask for the value, you get the value only of the first one.)

Does that help?
Regards,
Wendell

At 06:46 PM 9/27/01, you wrote:
Here's my XML:

<?xml version="1.0"?>
<!DOCTYPE Aps:VpmResultSet SYSTEM
"http://sofa.atc.lmco.com/assist/config/dtd/ApsVpmQueryResponse.dtd";>
<Aps:VpmResultSet xmlns:Aps="http://www.assist.com/";>
<Aps:PartStructure>
  <Aps:Name>assembly 1</Aps:Name>
  <Aps:PartNumber>1</Aps:PartNumber>
  <Aps:TopAssemblyName>pre assembly</Aps:TopAssemblyName>
  <Aps:AsDesigned>
     <Aps:Drawing>
        <Aps:Name>drawing assembly 1-1</Aps:Name>
        <Aps:DocumentId>314-1</Aps:DocumentId>
        <Aps:Sheets>
           <Aps:LocatorUrl>http://www.yahoo.com</Aps:LocatorUrl>
        </Aps:Sheets>
     </Aps:Drawing>
    <Aps:Drawing>
        <Aps:Name>drawing assembly 1-2</Aps:Name>
        <Aps:DocumentId>314-2</Aps:DocumentId>
        <Aps:Sheets>
           <Aps:LocatorUrl>http://www.yahoo.com</Aps:LocatorUrl>
        </Aps:Sheets>
     </Aps:Drawing>
  </Aps:AsDesigned>
</Aps:PartStructure>
<Aps:PartStructure>
  <Aps:Name>assembly 2</Aps:Name>
  <Aps:PartNumber>2</Aps:PartNumber>
  <Aps:TopAssemblyName>pre assembly</Aps:TopAssemblyName>
  <Aps:AsDesigned>
     <Aps:Drawing>
        <Aps:Name>drawing assembly 2</Aps:Name>
        <Aps:DocumentId>315</Aps:DocumentId>
        <Aps:Sheets>
           <Aps:LocatorUrl>http://www.yahoo.com</Aps:LocatorUrl>
        </Aps:Sheets>
     </Aps:Drawing>
  </Aps:AsDesigned>
</Aps:PartStructure>
</Aps:VpmResultSet>

Here's my XSL:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   xmlns:Aps="http://www.assist.com/";
   xmlns:xalan="http://xml.apache.org/xalan";
   exclude-result-prefixes="Aps">
  <xsl:output method="html" indent="yes"/>
  <xsl:param name="Name" select="."/>

  <xsl:template match="/">
    <xsl:for-each select="Aps:VpmResultSet/Aps:PartStructure">
      <xsl:for-each select="xalan:evaluate($Name)">
      <tr>
          <td><xsl:value-of
select="ancestor::Aps:VpmResultSet/Aps:PartStructure/Aps:Name"/></td>
         <td><xsl:value-of
select="ancestor::Aps:VpmResultSet/Aps:PartStructure/Aps:TopAssemblyName"/></td>

          <td><xsl:value-of
select="ancestor::Aps:VpmResultSet/Aps:PartStructure/Aps:PartNumber"/></td>

        <td><xsl:value-of select="Aps:DocumentId"/></td>
        <td><a><xsl:attribute name="href">
            <xsl:value-of select="Aps:Sheets/Aps:LocatorUrl"/>
            </xsl:attribute><xsl:value-of select="Aps:Name"/>
            </a>
        </td>
      </tr>
      </xsl:for-each>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

I'm getting (I've ignored the href part for now):
<tr>
<td>assembly 1</td>     <td>pre assembly</td>    <td>1</td>
<td>314-1</td>    <td>drawing assembly 1-1</td></tr>
<tr>
<td>assembly 1</td>     <td>pre assembly</td>    <td>1</td>
<td>314-2</td>    <td>drawing assembly 1-2</td> </tr>
<tr>
<td>assembly 1</td>     <td>pre assembly</td>    <td>1</td>
<td>315</td>    <td>drawing assembly 2</td></tr>

but what I want is:
<tr>
<td>assembly 1</td>     <td>pre assembly</td>    <td>1</td>
<td>314</td>    <td>drawing assembly 1</td></tr>
<tr>
<td>assembly 1</td>     <td>pre assembly</td>    <td>1</td>
<td>314-2</td>    <td>drawing assembly 1-2</td> </tr>
<tr>
<td>assembly 2</td>     <td>pre assembly</td>    <td>2</td>
<td>315</td>    <td>drawing assembly 2</td></tr>

The xalan:evaluate() is sent in from a setParameter("Name", param) and
seems to be working correctly.
In this case param would be Aps:AsDesigned/Aps:Drawing, however I've
just shown a section of the
true XML.  This param could be at different levels although the children
will look the same.

The xalan:evaluate section seems to work, it's just that I also need the
ancestor information in the
table and that doesn't seem to be doing what I thought it would.

Could someone please tell me why this doesn't work and how I should go
about fixing it?


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread