RE: [xsl] Select nodes with an attribute but not all the nodes

Subject: RE: [xsl] Select nodes with an attribute but not all the nodes
From: "Patrick Lachance" <plachance@xxxxxxxxx>
Date: Fri, 19 May 2006 12:45:16 -0400
Or if we can't use XSLT 2.0 ...

	<xsl:template match="level2[not(@attachpart)]">
        <record>
           <name>{name}</name>
            <attachingPart>
              <xsl:call-template name="attachingparts"/>
            </attachingPart>
        </record>
	</xsl:template>
	<xsl:template name="attachingparts">
	  <xsl:param name="current"
select="following::*[position()=1]"/>
        <xsl:param name="first" select="1"/>
        <xsl:if test="current/@attachpart">
          <xsl:if test="$first=0">
      	<xsl:text disable-output-escaping="yes">&#160;</xsl:text>
          </xsl:if>
          <xsl:value-of select="current/number"/>
          <xsl:call-template name="attachingparts">
		<xsl:with-param name="current"
select="current/following::*[position()=1]"/>
		<xsl:with-param name="first" select="0"/>
	    </xsl:call-template>
        </xsl:if>
      </xsl:template>

.. I think it could work

PL

-----Original Message-----
From: Michael Kay [mailto:mike@xxxxxxxxxxxx]
Sent: Friday, May 19, 2006 12:33 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: RE: [xsl] Select nodes with an attribute but not all the nodes

It's dangerous to use the word "when" in your problem statement because
it suggests you are thinking in terms of order of execution.

What you've got here is a positional grouping problem:

<xsl:for-each-group select="level2"
group-starting-with="*[not(@attachpart)]">
<record>
  <name><xsl:value-of select="name"/></name>
  <attachingPart>
    <xsl:value-of select="current-group()[@attachpart]/number"/>
  </attachingPart>
</record>
</xsl:for-each>

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: mysrdr-wrk@xxxxxxxxx [mailto:mysrdr-wrk@xxxxxxxxx]
> Sent: 19 May 2006 17:06
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Select nodes with an attribute but not all the nodes
>
> I am making an XML to XML transform using XSLT 2.0 and Saxon 8B.  I
> need to select certain nodes based on an attribute, but when I find a
> node without the attribute I want to stop selecting nodes even if
> subsequent siblings have the attribute.
>
>   <level2>
>               <name>box</name>
>               <number>1234</number>
>   </level2>
>   <level2>
>               <name attachpart="1">washer</name>
>               <number>234</number>
>   </level2>
>   <level2>
>               <name attachpart="1">screw</name>
>               <number>345</number>
>   </level2>
>   <level2>
>               <name attachpart="1">bolt</name>
>               <number>456</number>
>   </level2>
>   <level2>
>               <name>nameplate</name>
>               <number>9876</number>
>   </level2>
>   <level2>
>               <name>switch</name>
>               <number>7654</number>
>   </level2>
>   <level2>
>               <name attachpart="1">screw</name>
>               <number>345</number>
>   </level2>
>
>   The washer screw and bold are attaching parts for the box.
> I need to output these in an <attachingPart> element.
>
>   <record>
>               <name>box</name>
>               <attachingPart>234 345
> 456</attachingPart>
>   </record>
>
>   Depending on how I approach the code, I get nothing or just the
> washer or I get *all* of the parts that have the attachpart attribute.

> Is there some way to say stop selecting attachpart nodes when a level2

> doesn't have the attribute?  It's sounds so simple but I'm not getting

> anywhere with it.
>
> I've been reading both Michael and Jeni and checking the archives.
> Some things look close but I get the same results.
> The answer is probably right in front of my face but I've been looking

> at it so long I can't see the forest for the trees.
>
>   Regards,
>   Hermy

Current Thread