How do I use a regular expression in XSLT 1.0?

2019-01-24 02:20发布

问题:

I am using XSLT 1.0. My input information may contain these values

<!--case 1-->
<attribute>123-00</attribute>

<!--case 2-->
<attribute>Abc-01</attribute>

<!--case 3-->
<attribute>--</attribute>

<!--case 4-->
<attribute>Z2-p01</attribute>

I want to find out those string that match the criteria:

if string has at least 1 alphabet AND has at least 1 number,
then 
do X processing
else
do Y processing

In example above, for case 1,2,4 I should be able to do X processing. For case 3, I should be able to do Y processing.

I aim to use a regular expression (in XSLT 1.0).

For all the cases, the attribute can take any value of any length.

I tried use of match, but the processor returned an error. I tried use of translate function, but not sure if used the right way.

I am thinking about.

if String matches [a-zA-Z0-9]* 
then do X processing
else
do y processing.

How do I implement that using XSLT 1.0 syntax?

回答1:

This solution really works in XSLT 1.0 (and is simpler, because it doesn't and needn't use the double-translate method.):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>
 <xsl:variable name="vUpper" select=
 "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

 <xsl:variable name="vLower" select=
 "'abcdefghijklmnopqrstuvwxyz'"/>

 <xsl:variable name="vAlpha" select="concat($vUpper, $vLower)"/>

 <xsl:variable name="vDigits" select=
 "'0123456789'"/>

 <xsl:template match="attribute">
  <xsl:choose>
   <xsl:when test=
    "string-length() != string-length(translate(.,$vAlpha,''))
    and
     string-length() != string-length(translate(.,$vDigits,''))">

    Processing X
   </xsl:when>
   <xsl:otherwise>
    Processing Y
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML fragment -- made a well-formed XML document:

<t>
    <!--case 1-->
    <attribute>123-00</attribute>
    <!--case 2-->
    <attribute>Abc-01</attribute>
    <!--case 3-->
    <attribute>--</attribute>
    <!--case 4-->
    <attribute>Z2-p01</attribute>
</t>

the wanted, correct result is produced:

Processing Y


Processing X

Processing Y


Processing X

Do Note: Any attempt to use with a true XSLT 1.0 processor code like this (borrowed from another answer to this question) will fail with error:

<xsl:template match=
"attribute[
           translate(.,
                     translate(.,
                               concat($upper, $lower),
                               ''),
                     '')
         and
           translate(., translate(., $digit, ''), '')]
 ">

because in XSLT 1.0 it is forbidden for a match pattern to contain a variable reference.



回答2:

XSLT does not support regular expressions, but you can fake it.

The following stylesheet prints an X processing message for all attribute elements having a string value containing at least one number and at least one letter (and Y processing for those that do not):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:variable name="digit" select="'0123456789'"/>
    <xsl:template match="attribute">
        <xsl:choose>
            <xsl:when test="
                translate(., translate(., concat($upper, $lower), ''), '') and 
                translate(., translate(., $digit, ''), '')">
                <xsl:message>X processing</xsl:message>
            </xsl:when>
            <xsl:otherwise>
                <xsl:message>Y processing</xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Note: You said this:

In example above, for case 1,2,4 I should be able to do X processing. for case 3, I should be able to do Y processing.

But that conflicts with your requirement, because case 1 does not contain a letter. If, on the other hand, you really want to match the equivalent of [a-zA-Z0-9], then use this:

translate(., translate(., concat($upper, $lower, $digit), ''), '')

...which matches any attribute having at least one letter or number.

See the following question for more information on using translate in this way:

  • How to write xslt if element contains letters?


回答3:

If you found this question because you're looking for a way to use regular expressions in XSLT 1.0, and you're writing an application using Microsoft's XSLT processor, you can solve this problem by using an inline C# script.

I've written out an example and a few tips in this thread, where someone was seeking out similar functionality. It's super simple, though it may or may not be appropriate for your purposes.



标签: regex xslt