Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: [xsl] returning nodes which have a specific child

From: Michael Ludwig <mlu@---------.--->
To: xsl-list@-----.------------.---
Date: 7/2/2009 8:54:00 AM
jim mcgovern schrieb:
> Thanks very much for that!  Worked a treat.

Hi Jim,

glad to hear it helped.

> I'm relatively new to xsl and to be honest a lot of it seems like a
> black art.  Can you briefly describe what your solution does?

Sure, see comments inline.

>> <xsl:stylesheet version="1.0"
>>  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>>
>>  <xsl:template match="@*|node()"><!-- identity template -->
>>    <xsl:copy>
>>      <xsl:apply-templates select="@*|node()"/>
>>    </xsl:copy>
>>  </xsl:template>

The most important thing to understand is IMHO the so-called identity
template (also "copy template"), of which you see the canonical form
above. This copies the input unaltered. (The input is not the literal
XML, but the input tree, that is what the XSL processor makes of what
the XML parser produces - see XDM, XPath Data Model.)

I have an editor macro that produces the stylesheet skeleton plus
identity template, as this is almost always the right starting point
for a new stylesheet.

Note that XSLT has built-in template rules that basically lose the
markup and display the text only. Most of the time you'll want to
supplant these by the identity template. Some people even think that
the identity template should have been the built-in template rule.

Bottom line, start with the identity template.

>>  <xsl:template match="*" priority="-0.4" ><!-- skip unwanted -->
>>    <xsl:apply-templates select="*"/>
>>  </xsl:template>

Copying input to output is fine, but most of the time you want to make
some changes. So you insert other templates (rules) that take priority
over the identity template.

Priority is expressed as a decimal number. It is implicit from the match
pattern. In the above case, the match pattern has the same priority
(-0.5) as that of the identity template. So I give it an explicit
priority of -0.4 so that it takes precedence over the identity template
for matching nodes.

This template matches element nodes (*). So does the identity template
(by virtue of node(), which is a node test), but this one takes
precedence due to a higher priority.

It doesn't do much. You want to lose these nodes, but process their
child elements, so you don't copy anything and <apply-templates> to the
child elements, which are selected as "*". If I didn't process child
elements, well, processing would stop at this point in the input tree,
which is not what I want. I want processing to go all the way downtree.

>>  <xsl:template match="*[*[@name='CONTENT']]" ><!-- keep wanted -->
>>    <xsl:copy>
>>      <xsl:apply-templates select="@*|node()"/>
>>    </xsl:copy>
>>  </xsl:template>
>>
>> </xsl:stylesheet>

This last template is for the nodes you want to keep. The template
content should look familiar. Yes, that's the identity template
implementation again. You copy the element node and then process all
attributes (@*) and children (node()). They'll be picked up by either
the identity template, in which case their copied, or the skip template,
in which case their skipped.

Does that make things clearer?

Michael Ludwig

--~------------------------------------------------------------------
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@l...>
--~--



transparent
Print
Mail
Like It
Disclaimer
.

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 www.altova.com/list/index.html. 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.

.
.

transparent

transparent