How to filter state abbreviations from strings

I have XML where the number / forms / form / @ can be in almost any format (any combination of letters, numbers and spaces). Sometimes there will be @number values ​​that are the same form ... but are state specific. When this happens, the last 3 characters will be a single space, followed by an abbreviation for the state. So, the last two characters is an abbreviation of the state ... if it is present at all. I need to be able to select these "identical" forms for processing.

XSLT 2.0 is fine. I think the solution will include keys, or perhaps grouping methods, but I'm at a loss. NOTE. I still need to iterate over forms that do not have matches for a particular state in one cycle ... I just have to do something special for those with several states. Of course, I do not control XML.

<forms>
<!-- THESE 3 WOULD MATCH -->
<form number="ABC 12 45"/>
<form number="ABC 12 45 IL"/>
<form number="ABC 12 45 KY"/>
<!-- 2 OF 3 WOULD MATCH AS ZZ IS NOT A STATE -->
<form number="CGF 45"/>
<form number="CGF 45 ZZ"/>
<form number="CGF 45 IL"/>
<!-- THESE 3 WOULD MATCH -->
<form number="955EZ IL"/>
<form number="955EZ MN"/>
<form number="955EZ CA"/>
<!-- NO MATCHES -->
<form number="25 AB 4"/>
<form number="SR HL DR"/>
</forms>
+3
source share
1 answer

Just use :

<xsl:template match=
  "form[substring(@number, string-length(@number) -2, 1)=' '
      and
       translate(substring(@number, string-length(@number)-1),
                 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                ) = ''
      and
       contains('|AL|AK|AZ|AR|...|WA|WI|WY|',
                concat('|',substring(@number, string-length(@number)-1),'|')
                )
       =
        ''
       ]">

<!-- Needed processing here -->

</xsl:template>

XSLT 2.0 Solution :

<xsl:template match=
  "form[substring(@number, string-length(@number) -2, 1)=' '
      and
       substring(@number, string-length(@number)-1)
      =
       ('AL','AK','AZ','AR',...,'WA','WI','WY') 
       ]">

<!-- Needed processing here -->

</xsl:template>
+1
source

Source: https://habr.com/ru/post/1793705/


All Articles