xslt - Differentiate between capital letters and big roman numerals -


i've below xml document.

<root> <toc-subitem><toc-title>(c) 1 year&#x2019;s separation consent (s 11a(c))</toc-title> <toc-pg>1.055</toc-pg> <toc-subitem><toc-title>(i) rescission of decree <content-style font-style="italic">nisi</content-style></toc-title>  <toc-pg>1.062</toc-pg></toc-subitem></toc-subitem> </root> 

here i'm trying differentiate between roman numerals , capital letters using below xslt template.

<xsl:template name="get_number_type"> <xsl:param name="number_string"/>  <xsl:analyze-string select="$number_string" regex="([0-9]+\.)|(\([a-h]\))|(\([ivx]+\))|(\([a-z]+\))|(\([ivxl]+\))">    <xsl:matching-substring>     <xsl:choose>       <xsl:when test="regex-group(1) != ''">         <xsl:text>1</xsl:text>       </xsl:when>       <xsl:when test="regex-group(2) != ''">         <xsl:text>2</xsl:text>       </xsl:when>       <xsl:when test="regex-group(3) != ''">         <xsl:text>3</xsl:text>       </xsl:when>       <xsl:when test="regex-group(4) != ''">         <xsl:text>4</xsl:text>       </xsl:when>       <xsl:when test="regex-group(5) != ''">         <xsl:text>5</xsl:text>       </xsl:when>     </xsl:choose>   </xsl:matching-substring>   <xsl:non-matching-substring>   </xsl:non-matching-substring>  </xsl:analyze-string> 

the param value substring-before(./toc-title,' ')/>

the expected output 4 (c) , 5 (i) in case showing 4 both cases.

please let me know how can differentiate these 2 cases.

thanks.

while testing [abd-hj-wyz] adequate solution, instead use character class subtraction.

for example, input document ...

<root> <toc-subitem><toc-title>(c) 1 year&#x2019;s separation consent (s 11a(c))</toc-title> <toc-pg>1.055</toc-pg> <toc-subitem><toc-title>(i) rescission of decree <content-style font-style="italic">nisi</content-style></toc-title>  <toc-pg>1.062</toc-pg></toc-subitem></toc-subitem> </root> 

... xslt 2.0 stylesheet ...**

<xsl:transform     xmlns:xsl="http://www.w3.org/1999/xsl/transform"      xmlns:so="http://stackoverflow.com/questions/24448301"     xmlns:xs="http://www.w3.org/2001/xmlschema"     version="2.0"     exclude-result-prefixes="so xs"> <xsl:output omit-xml-declaration="yes" encoding="utf-8" indent="yes" /> <xsl:strip-space elements="*" />  <xsl:template match="@*|node()">   <xsl:copy>     <xsl:apply-templates select="@*|node()"/>   </xsl:copy> </xsl:template>  <xsl:function name="so:group" as="xs:integer?">   <xsl:param name="number_string" as="xs:string" />   <xsl:analyze-string select="$number_string"       regex="([0-9]+\.)            |              (\([a-h]\))           |              (\([ivx]+\))          |              (\(([a-z-[ivxl]])+\)) |              (\([ivxl]+\))          "       flags="x">     <xsl:matching-substring>       <xsl:choose>         <xsl:when test="regex-group(1)">           <xsl:sequence select="1"/>         </xsl:when>         <xsl:when test="regex-group(2)">           <xsl:sequence select="2"/>         </xsl:when>         <xsl:when test="regex-group(3)">           <xsl:sequence select="3"/>         </xsl:when>         <xsl:when test="regex-group(4)">           <xsl:sequence select="4"/>         </xsl:when>         <xsl:when test="regex-group(6)">           <xsl:sequence select="5"/>         </xsl:when>       </xsl:choose>     </xsl:matching-substring>   </xsl:analyze-string> </xsl:function>  <xsl:template match="toc-subitem">   <xsl:copy>     <xsl:apply-templates select="@*"/>     <xsl:attribute name="regex-group">       <xsl:value-of select="so:group(substring-before(.,' '))" />     </xsl:attribute>     <xsl:apply-templates select="node()"/>   </xsl:copy> </xsl:template>  </xsl:transform> 

... yield output ...

<root>    <toc-subitem regex-group="4">       <toc-title>(c) 1 year’s separation consent (s 11a(c))</toc-title>       <toc-pg>1.055</toc-pg>       <toc-subitem regex-group="5">          <toc-title>(i) rescission of decree <content-style font-style="italic">nisi</content-style>          </toc-title>          <toc-pg>1.062</toc-pg>       </toc-subitem>    </toc-subitem> </root> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -