Re: [xsl] How to do macro substitution in XPath?

Subject: Re: [xsl] How to do macro substitution in XPath?
From: "G. Ken Holman g.ken.holman@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 2 Dec 2023 16:07:21 -0000
How about simple Boolean variables?

  <xsl:variable name="LBJ" select="..." as="xsd:boolean"/>
  <xsl:variable name="RM" select="..." as="xsd:boolean"/>
  ...
  <xsl:value-of select="if( $LBJ ) then 'action 1'
                        else if( $RM ) then 'action 2'
                        else 'action 3'"/>

Strictly speaking the as= isn't necessary since the evaulation of the expression is Boolean.

I hope this helps.

. . . . Ken

At 2023-12-02 15:36 +0000, Roger L Costello costello@xxxxxxxxx wrote:
Hi Folks,

I am converting prose like this:

If the title = "Path to Power" and the author = "Robert A. Caro" (Lyndon Baines Johnson (LBJ)), then
process LBJ as follows
action 1
If the title = "Power Broker" and the author = "Robert A. Caro" (Robert Moses (RM)), then
process RM as follows
action 2
Neither LBJ nor RM
action 3


I need the XPath to be as like the prose as possible.

Suppose the XPath will operate on XML documents like this:

<Book>
  <Title>Path to Power</Title>
  <Author>Robert A. Caro</Author>
</Book>

Below is one approach to writing the XPath. See the XPath in the xsl:value-of element. Notice the use of XML ENTITIES.

The advantage of that approach is the XPath looks like the prose.

The disadvantage with that approach is that ENTITIES are not XPath. I need to stick with pure XPath. What XPath do you recommend?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet[
<!ENTITY LBJ "((/Book/Title eq 'Path to Power') and (/Book/Author eq 'Robert A. Caro'))">
<!ENTITY RM "((/Book/Title eq 'Power Broker') and (/Book/Author eq 'Robert A. Caro'))">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="3.0">
<xsl:template match="/">
<xsl:value-of select="
if &LBJ; then
'action 1'
else if &RM; then
'action 2'
else if (not((&LBJ;) or (&RM;))) then
'action 3'
else
'Error'
"/>
</xsl:template>
</xsl:stylesheet>




--
Contact info, blog, articles, etc. http://www.CraneSoftwrights.com/s/ |
Check our site for free XML, XSLT, XSL-FO and UBL developer resources |
Streaming hands-on XSLT/XPath 2 training class @US$125 (5 hours free) |
Essays (UBL, XML, etc.) http://www.linkedin.com/today/author/gkholman |

Current Thread