xslt - 为每个匹配增加文本字符串中的数字
问题描述
我希望通过查看 XSLT 是否可以为每个匹配项增加文本编号来缩短我的 XSLT 代码库。文本编号存在于属性值“label-period0”和“xls:value-of”值中。
代码有效,没有错误,所以这更多的是如何缩短代码并利用字符串中特定字符的某种迭代的问题。
我为“period0”和“period1”添加了 2 个类似的代码结构,以更好地查看文本字符串中数字方面所需的确切更改。
源 XML 文件:
<data>
<periods>
<period0><from>2016-01-01</from><to>2016-12-01</to></period0>
<period1><from>2015-01-01</from><to>2015-12-01</to></period1>
<period2><from>2014-01-01</from><to>2014-12-01</to></period2>
<period3><from>2013-01-01</from><to>2013-12-01</to></period3>
</periods>
<balances>
<balance0><instant>2016-12-31</instant></balance0>
<balance1><instant>2015-12-31</instant></balance1>
<balance2><instant>2014-12-31</instant></balance2>
<balance3><instant>2013-12-31</instant></balance3>
</balances>
</data>
XSL 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform
version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" indent="yes"/>
<!-- Block all data that has no user defined template -->
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="data">
<results>
<periods>
<periods label="period0">
<xsl:value-of
select =
"concat(periods/period0/from, '--', periods/period0/to)"
/>
</periods>
<periods label="period1">
<xsl:value-of
select =
"concat(periods/period1/from, '--', periods/period1/to)"
/>
</periods>
<!-- Etc for period [2 and 3]-->
</periods>
<balances>
<balance label="balance0">
<xsl:value-of select ="balances/balance0/instant"/>
</balance>
<!-- Etc for balance [1,2 and 3] -->
</balances>
</results>
</xsl:template>
</xsl:transform>
结果:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<periods>
<periods label="period0">2016-01-01--2016-12-01</periods>
<periods label="period1">2015-01-01--2015-12-01</periods>
</periods>
<balances>
<balance label="balance0">2016-12-31</balance>
</balances>
</results>
想要的结果:
(使用 XSL 对文本字符串中的数字进行步进,或 XSL 中可以满足操作文本字符串中数字的任何其他逻辑)
<?xml version="1.0" encoding="UTF-8"?>
<results>
<periods>
<periods label="period0">2016-01-01--2016-12-01</periods>
<periods label="period1">2015-01-01--2015-12-01</periods>
<periods label="period2">2014-01-01--2015-12-01</periods>
<periods label="period3">2013-01-01--2015-12-01</periods>
</periods>
<balances>
<balance label="balance0">2016-12-31</balance>
<balance label="balance1">2015-12-31</balance>
<balance label="balance2">2014-12-31</balance>
<balance label="balance3">2013-12-31</balance>
</balances>
</results>
解决方案
你不能简单地做类似的事情:
<xsl:template match="/data">
<results>
<periods>
<xsl:for-each select="periods/*">
<periods label="{name()}">
<xsl:value-of select="from"/>
<xsl:text>--</xsl:text>
<xsl:value-of select="to"/>
</periods>
</xsl:for-each>
</periods>
<balances>
<xsl:for-each select="balances/*">
<balance label="{name()}">
<xsl:value-of select="instant"/>
</balance>
</xsl:for-each>
</balances>
</results>
</xsl:template>
如果你想做自己的编号,你可以改变:
<periods label="{name()}">
至:
<periods label="period{position() - 1}">
推荐阅读
- lotus-notes - IBM Domino - 删除共享视图并将 NSF 应用程序转换为 NTF(模板)
- node.js - 是否可以在未经用户授权的情况下获取公共的 Spotify 播放列表?
- karate - 如何使用空手道框架用 def 值替换键
- mysql - mysql查询在一行中打印不同行的数据
- firebase - 如何将 JSON 导入 Cloud Firestore
- powerbi - Power Bi DAX:累积和未显示正确数据
- node.js - 可以使用 Heroku 上的流下载 TXT 文件,但无法下载 PDF 文件 - NodeJS、Heroku
- swift - 如何正确地将 ImagePicker 插入标题中的图像?SwiftUI
- realm - 从 webRTC 客户端为 coTurn 提供多个领域/来源
- python - 如何从循环中创建的多个条目小部件中获取输入?