How do I use a regular expression in XSLT 1.0?

前端 未结 3 1339
一个人的身影
一个人的身影 2020-12-16 13:33

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


123-00




        
相关标签:
3条回答
  • 2020-12-16 13:41

    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.

    0 讨论(0)
  • 2020-12-16 13:41

    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?
    0 讨论(0)
  • 2020-12-16 13:43

    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.

    0 讨论(0)
提交回复
热议问题