Re: [xsl] selecting w:p nodes based on w:pStyle attributes

Subject: Re: [xsl] selecting w:p nodes based on w:pStyle attributes
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Mon, 27 Mar 2006 17:17:51 -0500
At 2006-03-27 16:41 -0500, Terry Ofner wrote:
I am trying to select w:p nodes based on w:pStyle attribute.
...
The p:Style node is a child of the w:p node

No, it is a descendant, it is not a child in your example.


which I want to select and copy to the result tree. Here is an example of the XML:
...


<w:p>
      <w:pPr>
        <w:pStyle w:val="A-Head"/>
      </w:pPr>
      <w:r>
        <w:t>Nouns</w:t>
      </w:r>
</w:p>
<w:p>
      <w:pPr>
        <w:pStyle w:val="NL"/>
      </w:pPr>
      <w:r>
        <w:t>blah blah blah</w:t>
      </w:r>
</w:p>
...
  <xsl:template match="w:p/w:pPr/w:pStyle[@w:val = 'NL']">

There you are matching on the descendant of w:p, not on w:p, yet your first sentence quoted above is that you are trying to select w:p ... but you don't say what you want done with the w:p that you select.


I know that "w:p/w:pPr/w:pStyle[@w:val = 'NL']" works to select the particular attribute.

No, that expression in a select= attribute selects w:pStyle based on the particular attribute, it does not select the attribute.


For example, when I run the template below, I get repeated <w:pStyle w:val = "NL" > nodes in the result tree with nothing else.

 <xsl:template match="w:p"/>
   <xsl:apply templates select="w:pPr/w:pStyle[@w:val = 'NL']"/>

</xsl:template>

What am I doing wrong?

You don't say what you want to do with the w:p elements you said you wanted to select.


Guessing what your needs might be, I suspect you want:

 <xsl:template match="w:p[w:pPr/w:pStyle/@w:val = 'NL']">
   <!--do something here with w:p as current node-->
 </xsl:template>

Your issue is that you don't want to walk away from what it is you want to select ... that means that you add the predicate to what you want to select, as I did in the above example. A synonymous match statement would also be:

 <xsl:template match="w:p[w:pPr/w:pStyle[@w:val = 'NL']]">
   <!--do something here with w:p as current node-->
 </xsl:template>

Notice in my answer to your earlier question that I did the work in the template that matches the node needed:

http://www.biglist.com/lists/xsl-list/archives/200603/msg00735.html

So the same applies above: match the node you want to work with by qualifying it with a predicate. Don't use "/" to walk away from the node.

I hope this helps.

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


-- Registration open for XSLT/XSL-FO training: Wash.,DC 2006-06-12/16 World-wide on-site corporate, govt. & user group XML/XSL training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Aug'05 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread