2016-01-24 140 views
1

的数值我有XML文件中像下面,XSLT - 获取文本()节点

<sec> 
    <para>Section 1- TOC</para> 
    <para>Section 4* Main</para> 
    <para>Section 3$ Basic content</para> 
    <para>Section 11_ Section 10</para> 
    <para>Section [email protected] Appendix 6</para> 
</sec> 

我需要得到其次是“科”在text()节点使用功能串的数量。

例如:

<xsl:function name="abc:get-section-number"> 
     <xsl:param name="string" as="xs:string"/> 

     <xsl:sequence select="tokenize($string,'\d+')[1]"/> 
    </xsl:function> 

此示例返回数值前的子字符串,但我需要得到的数值后“部分的”串..(输出值应该是1,4,3,11和15)

我尝试了一些内置函数(string-before,strong-after,matches ..)但找不到任何合适的解决方案。

任何人都可以建议我一个方法来获得这个数字值吗?

+1

检查[这个答案](http://stackoverflow.com/a/34807281/3832970)工作示例。你似乎只需要'Section \ s +(\ d +)'。然后'' –

回答

2

您可以使用analyze-string,作为一个评论已经建议,看看为此做

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="2.0" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:abc="http://example.com/abc" 
    exclude-result-prefixes="xs abc"> 

    <xsl:function name="abc:get-section-number" as="xs:integer"> 
     <xsl:param name="string" as="xs:string"/> 
     <xsl:analyze-string select="$string" regex="^Section\s+([0-9]+)"> 
      <xsl:matching-substring> 
       <xsl:sequence select="xs:integer(regex-group(1))"/> 
      </xsl:matching-substring> 
     </xsl:analyze-string> 
    </xsl:function> 

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="para"> 
     <xsl:copy> 
      <xsl:value-of select="abc:get-section-number(.)"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:transform>