XSLT:将文本子字符串转换为整数值

问题描述 投票:0回答:1
xslt-2.0
1个回答
0
投票

考虑发布一个最小但完整的示例,以及所使用的 XSLT 处理器的详细信息,以允许其他人重现该错误;根据您的问题和评论,我创建了运行 SaxonC HE 12.3 的在线示例并执行例如

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:template match="root">
    <xsl:for-each select="p[@outputclass = 'Heading' and starts-with(., 'Cycles ')]">
      <xsl:variable name="cycle_end_string" select="substring-after(., '–')"/>
      <xsl:variable name="cycle_end_number" select="number($cycle_end_string)"/>
      <xsl:variable name="cycle_end_integer" select="xs:integer($cycle_end_number)"/>
      <div>{$cycle_end_integer}</div>
    </xsl:for-each>    
  </xsl:template>

  <xsl:output method="xml" indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} at {current-dateTime()}</xsl:comment>
  </xsl:template>

</xsl:stylesheet>

针对输入

<root>
<p outputclass="Heading">Cycles 1–5</p>
<p outputclass="Heading">Cycles 6–10</p>
<p outputclass="Heading">Cycles 11–16</p>
</root>

并且它不输出任何错误,但例如

<?xml version="1.0" encoding="UTF-8"?>
<div>5</div>
<div>10</div>
<div>16</div>
<!--Run with SAXON HE 12.3 at 2023-11-25T23:08:29.498723Z-->

就好了。

© www.soinside.com 2019 - 2024. All rights reserved.