XSLT - 获取 text() 节点的数字值
XSLT - get number value of the text() node
我有 XML 如下文件,
<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 15@ Appendix 6</para>
</sec>
我需要使用函数在 text()
节点中获取数字后跟 'Section' 字符串。
示例:
<xsl:function name="abc:get-section-number">
<xsl:param name="string" as="xs:string"/>
<xsl:sequence select="tokenize($string,'\d+')[1]"/>
</xsl:function>
此示例 returns 数字值之前的子字符串,但我需要在 'Section' 字符串之后获取数字值。(输出值应为 1、4、3、11 和 15)
我尝试了一些内置函数(string-before、strong-after、matches..)但找不到任何合适的解决方案。
谁能建议我一个获取这个数值的方法?
您可以使用 analyze-string
,正如评论中已经建议的那样,请参阅 http://xsltransform.net/3NJ38Zy 的工作示例
<?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>
我有 XML 如下文件,
<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 15@ Appendix 6</para>
</sec>
我需要使用函数在 text()
节点中获取数字后跟 'Section' 字符串。
示例:
<xsl:function name="abc:get-section-number">
<xsl:param name="string" as="xs:string"/>
<xsl:sequence select="tokenize($string,'\d+')[1]"/>
</xsl:function>
此示例 returns 数字值之前的子字符串,但我需要在 'Section' 字符串之后获取数字值。(输出值应为 1、4、3、11 和 15)
我尝试了一些内置函数(string-before、strong-after、matches..)但找不到任何合适的解决方案。
谁能建议我一个获取这个数值的方法?
您可以使用 analyze-string
,正如评论中已经建议的那样,请参阅 http://xsltransform.net/3NJ38Zy 的工作示例
<?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>