Search code examples
xmlxsltxslt-2.0xslt-3.0

How can I use an XSLT variable containing a pattern in a template match expression


I would like to do

<xsl:variable name="myPattern" select="node1|node2"/>
<xsl:template match="$myPattern">
    ...
</xsl:template>
<xsl:template  match="/">
    ...
    <xsl:for-each select="distinct-values(//$myPattern/name/text()">
        ...
    </xsl:for-each>
</xsl:template>

I tried this with XSLT version 2.0 and 3.0 to no avail. Any hints?

Reason: The pattern is a little more complicated and I would like to use it in several places and not just this match.

EDIT:

I solved my problem for now by accepting the fact that the variable does not contain the string/pattern, but the result nodes. If I modify it to

<xsl:variable name="myNodes" select="//(node1|node2)"/>
<xsl:template match="$myNodes">
    ...
</xsl:template>
<xsl:template  match="/">
    ...
    <xsl:for-each select="distinct-values($myNodes/name/text()">
        ...
    </xsl:for-each>
</xsl:template>

it works fine.

I still wonder why it is not possible to simply store the string in the variable and use it wherever literal strings are allowed.


Solution

  • As for textual replacement, with XSLT 3.0 you can use use a static parameter with a string value and then so called shadow attributes (https://www.w3.org/TR/xslt-30/#shadow-attributes):

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:math="http://www.w3.org/2005/xpath-functions/math" exclude-result-prefixes="xs math"
        version="3.0">
    
        <xsl:param name="myPattern" static="yes" as="xs:string" select="'node1|node2'"/>
    
        <xsl:template _match="{$myPattern}">
            <matched name="{node-name()}">
                <xsl:apply-templates/>
            </matched>
        </xsl:template>
    
        <xsl:template match="/*">
            <xsl:copy>
                <xsl:for-each _select="distinct-values(//{$myPattern}/text())">
                    <value>
                        <xsl:value-of select="."/>
                    </value>
                </xsl:for-each>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    That transforms

    <root>
        <node1>a</node1>
        <node2>1</node2>
        <node1>a</node1>
    </root>
    

    into

    <root><value>a</value><value>1</value>
            <matched name="node1">a</matched>
            <matched name="node2">1</matched>
            <matched name="node1">a</matched>
    </root>
    

    In XSLT 3.0 you can use a variable or parameter reference for the match pattern of a template but it is not a textual replacement that happens, rather "$xyz matches any node that is present in the value of the variable $xyz" (https://www.w3.org/TR/xslt-30/#pattern-examples).

    So with the XSLT being

    <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
        <xsl:param name="delete" select="//*[contains-token(@class, 'foo')]"/>
    
        <xsl:mode on-no-match="shallow-copy"/>
    
        <xsl:template match="$delete"/>
    
    </xsl:stylesheet>
    

    and the XML input being

    <html>
        <head>
            <title>test</title>
        </head>
        <body>
            <p class="foobar bar">Paragraph 1.</p>
            <p class="foo bar">Paragraph 2.</p>
            <p class="bar">Paragraph 3.</p>
            <p class="foo">Paragraph 4.</p>
        </body>
    </html>
    

    a conforming XSLT 3.0 processor like Saxon 9.7 EE outputs

    <html>
        <head>
            <title>test</title>
        </head>
        <body>
            <p class="foobar bar">Paragraph 1.</p>
    
            <p class="bar">Paragraph 3.</p>
    
        </body>
    </html>