Re: [xsl] Ways to Identify & Process Same name XML elements differently based on location in XML tree?

Subject: Re: [xsl] Ways to Identify & Process Same name XML elements differently based on location in XML tree?
From: "Wendell Piez wapiez@xxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 26 Oct 2017 14:20:38 -0000
Hi,

Then too, some rudimentary XPath might be used to distinguish between
elements in different contexts even in a single pass ... please
forgive if this is obvious

match="dict" matches any dict w/ @priority=0
match="dict//dict" matches any dict that appears inside another dict,
with @priority="0.5"

So in an XSLT with templates matching both patterns, an "outermost
dict" can be distinguished from any other because only outermost dict
elements match the first template, but not the second.

The same distinction can be made for match="array" (matching any array
element) and match="array/array" (only an array whose parent is an
array) etc. etc.

I know this is rudimentary stuff but it's surprising to see sometimes
the conniptions in stylesheets written by people unaware they could do
this.

Advice to newbies and refresher-learners: go read a guide on match
patterns in XSLT, including the rules on assignment of priorities
(@priority) explicity or by default ....

Cheers, Wendell




On Wed, Oct 25, 2017 at 8:53 AM, Graydon graydon@xxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> On Wed, Oct 25, 2017 at 12:34:55PM -0000, Alex S as.signup@xxxxxxxxxxx
> scripsit:
>>  Some elements such as: <string> <array> <dict> appear repeatedly at
>>  multiple levels in this SB XML format, But they mean different things
>>  based on where they are located; mean different things at different
>>  locations, depths or parent/ ascendent
>>
>>  What are the different ways I can tackle processing these elements
>>  and fix & improve my <xsl templates>?
>
> There's a general XSLT 2.0-and-greater pattern, "chained processing",
> where you can put a document node in a variable so you can pass the
> result of one processing pass to the next using variables:
>
> <xsl:variable name="pass1">
>     <xsl:apply-templates mode="preprocess"/>
> </xsl:variable>
> <xsl:apply-templates select="pass1"/>
>
> This generalizes to arbitrarily many variables (presuming the documents
> aren't large enough to give you memory issues at however many variables
> you need) and I find it's a good way to keep different bits of the
> processing logic distinct in the code and thus easier to debug.
>
> So nothing wrong with doing what you're already doing and using a
> pre-processing pass to give the elements distinct names depending on
> position and then processing on that basis.
>
> -- Graydon
> 



-- 
Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables
_____oo_________o_o___ooooo____ooooooo_^

Current Thread