Re: [xsl] How should I handle very similar templates ? Inserting a string into a match spec ?

Subject: Re: [xsl] How should I handle very similar templates ? Inserting a string into a match spec ?
From: "Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 21 Apr 2023 15:33:38 -0000
A somewhat different solution, and possibly made unnecessary with the newer
options Martin describes, is to define a function that checks your conditions
and then use that function in your match statement. I usually use this
approach when the match logic is complicated or needs to do more data
processing (like look something up using a key or whatever).

One advantage of the function approach is that it enables overriding the
function so different uses of the base template could use different match
logic by providing their own function implementation. So if youbre writing
XSLT that is intended to be used and extended, using functions for match
conditions could be appropriate or necessary.

Cheers,

E.

_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com<https://www.servicenow.com>
LinkedIn<https://www.linkedin.com/company/servicenow> |
Twitter<https://twitter.com/servicenow> |
YouTube<https://www.youtube.com/user/servicenowinc> |
Facebook<https://www.facebook.com/servicenow>

From: Richard Kerry richard.kerry@xxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thursday, April 20, 2023 at 6:54 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject: [xsl] How should I handle very similar templates ? Inserting a string
into a match spec ?
[External Email]

________________________________

I am finding myself writing a number of templates that are almost identical,
differing only in strings within the match spec, and I'd like to reduce the
near-duplication, if it is possible.
I am filtering some Visual Studio project files to remove a certain
configuration and remove mentions of the remaining one.  The files include
elements that start as follows (slightly simplified, in case anyone familiar
with VS is saying "that's not quite right"):

  <PropertyGroup Condition="'$(Configuration)'=='Debug|Win32'" >
or

  <PropertyGroup Condition="'$(Configuration)'=='Release|Win32'" >
My task is to process one of these files and remove all references to either
Debug or Release.  I will remove all elements with the unwanted configuration,
and remove all the attributes that mention the wanted one.
I have templates to remove these elements, or to remove all the attriutes that
mention a particular configuration.
So one stylesheet contains a template to detect Release and remove all
elements (ie not copy them), and a template to remove all Debug configuration
attributes.

Templates are simple in that they do nothing in order to delete the element or
attribute from the output.

bbbbbb<xsl:template match="*[ @Condition =
'''$(Configuration)''==''Release|Win32''' ]" />

bbbbbb<xsl:template match="@Condition[ .=
'''$(Configuration)''==''Debug|Win32''' ]" />

Applying these two will remove all Release elements, and all mentions of Debug
from remaining elements.
I have another stylesheet that does the opposite, ie delete Debug elements and
Release attributes.

What I'd like to do is pass the keywords in as parameters; something like
bbbbbb<xsl:template match="*[ @Condition =
'''$(Configuration)''==''{$delete-element-name}|Win32''' ]" />

bbbbbb<xsl:template match="@Condition[ .=
'''$(Configuration)''==''{$delete-attribute-name}|Win32''' ]" />

with appropriate variables or parameters declared.  I tried a couple of
variants of the above, to no avail.
But I'm not even sure if this is possible.  Is it?  If it is possible but I
need to approach it differently, please can someone advise.

If this was C++ I'd just pass in strings and build the match specs
accordingly, or if it was more complex I'd put some logic in a class and pass
an object.



Richard Kerry
BNCS Engineer, SI SOL Telco & Media Vertical Practice
M: +44 (0)7812 325518
2nd Floor, MidCity Place, 71 High Holborn, London, WC1V 6EA
richard.kerry@xxxxxxxx<https://webmail.siemens-it-solutions.com/owa/redir.asp
x?C=9fb20d019e3e4cb99344d708709a3177&URL=mailto%3arichard.kerry%40atos.net>



This e-mail and the documents attached are confidential and intended solely
for the addressee; it may also be privileged. If you receive this e-mail in
error, please notify the sender immediately and destroy it. As its integrity
cannot be secured on the Internet, the Atos group liability cannot be
triggered for the message content. Although the sender endeavours to maintain
a computer virus-free network, the sender does not warrant that this
transmission is virus-free and will not be liable for any damages resulting
from any virus transmitted.
XSL-List info and archive<http://www.mulberrytech.com/xsl/xsl-list>
EasyUnsubscribe<http://lists.mulberrytech.com/unsub/xsl-list/3453418> (by
email<>)

Current Thread