RE: p:label-elements vs p:add-attribute

Hi David,

A difference worth considering is that p:add-attributes/@attribute-value is a string (literal), while p:label-elements/@label is an XPath expression to be evaluated in context.

If your value needs to be calculated in its local scope, that is, you need p:label-elements. If XProc functions, variables etc. are enough, you can use p:add-attribute with AVT syntax { }.

This is essentially why you had to use p:viewport around your p:add-attribute, to give you access to the document context - using p:label-elements you can simply match what you need to match.

If you dig around in my tutorial repo you'll find some p:label-elements, in some places as a workaround since I didn't know of the existence of p:make-absolute-uris.

Cheers, Wendell

-----Original Message-----
From: Martin Honnen <martin.honnen@gmx.de> 
Sent: Wednesday, February 5, 2025 5:40 PM
To: xproc-dev@w3.org
Subject: Re: p:label-elements vs p:add-attribute


On 05/02/2025 23:01, David Birnbaum wrote:
> Dear XProc-dev,
>
> I'm still learning my way around XProc, and to add unique identifier 
> attributes to specific elements in a document within a pipeline I used 
> <p:viewport> to match the elements I cared about and <p:add-attribute> 
> (using p:iteration-position()) to create and add the unique 
> identifiers. I was pleased with the result until I stumbled today over 
> <p:label-elements>, which I hadn't noticed / known about earlier, I 
> see that <p:label-elements> has a "replace" option that 
> <p:add-attribute> lacks, but otherwise it looks as if the two methods 
> can be used for the same purpose.
>
> Are there situations (other than where I need to control whether to 
> replace an existing value) where one method should be preferred over 
> the other? That is, what types of use-cases motivated the decision to 
> make both methods available in the language? Thanks for any guidance!
>

Perhaps one advantage that p:label-elements gives you is that positional index variable "An additional variable $p:index is available that holds the index (sequence number) of the match" exposed in the "label" expression.

Received on Thursday, 6 February 2025 14:43:20 UTC