区分大写字母和大罗马数字

时间:2014-06-27 09:23:31

标签: xslt

我有以下XML文档。

<root>
<toc-subitem><toc-title>(C) One year&#x2019;s separation with 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模板下面的罗马数字和大写字母区分。

<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>

param值为substring-before(./toc-title,' ')/>

4的预期输出为(C)5的预期输出为(I),在这种情况下,两种情况都显示4

请告诉我如何区分这两种情况。

感谢。

1 个答案:

答案 0 :(得分:0)

虽然测试[ABD-HJ-WYZ]是一个非常合适的解决方案,但您可以使用字符类减法。

例如,使用输入文档...

<root>
<toc-subitem><toc-title>(C) One year&#x2019;s separation with 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样式表...... **

<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>

...会产生输出......

<root>
   <toc-subitem regex-group="4">
      <toc-title>(C) One year’s separation with 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>