简体   繁体   中英

How to test if an xml tag is 3 char long and uppercase with XSL

I am trying to transform an XML file with XSL and I need to test if a tag is 3 character long and uppercase. The XML file I am trying to transform (and I have no control over the format of this) looks like this:

<RECORD>
   <AAA>
       <c01>1</c01>
       <c02>2</c02>
   </AAA>
   <BBB>
       <c01>3</c01>
       <c02>4</c02>
   </BBB>
</RECORD>

And I need to separate the AAA and BBB tags with ":" so I get something like

AAA:1+2;BBB:3+4;

However, AAA and BBB can be practically any 3 letter combination. Otherwise I could have done something like this.

<xsl:param name="X">AAA:BBB:CCC</xsl:param>
<xsl:for-each select="//*[contains($X, substring(local-name(), 1, 3))]">

I have found xsl functions uppercase and lowercase, but nothing to test the case in a simple way. Does anyone know how to achieve this, without making it too complicated?

If you can use XSLT 2.0, you can use the matches function to make use of regular expressions

 <xsl:for-each select="//*[matches(local-name(), '^[A-Z]{3}$')]">

In XSLT 1.0, it is a little more work, but you could do something like this...

<xsl:for-each select="//*[string-length(local-name()) = 3 and translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '') = '']">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM