gpt4 book ai didi

xml - xslt 1.0 中的拆分函数

转载 作者:数据小太阳 更新时间:2023-10-29 01:40:51 25 4
gpt4 key购买 nike

如何在 XSLT 1.0 中拆分节点值?

<mark>1,2</mark>

我需要在 for 循环中对 split 输出的每个值执行一些操作。

<xsl:for-each select="">
</xsl:for-each>

如何做到这一点?

最佳答案

我。 XSLT 1.0 解决方案:

这是在 XSLT 1.0 中仅使用 xxx:node-set() 扩展函数 执行此操作的一种方法:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="mark">
<xsl:variable name="vrtfSplit">
<xsl:apply-templates/>
</xsl:variable>

<xsl:for-each select="ext:node-set($vrtfSplit)/*">
<processedItem>
<xsl:value-of select="10 * ."/>
</processedItem>
</xsl:for-each>
</xsl:template>

<xsl:template match="text()" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText) >0">
<item>
<xsl:value-of select=
"substring-before(concat($pText, ','), ',')"/>
</item>

<xsl:call-template name="split">
<xsl:with-param name="pText" select=
"substring-after($pText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<mark>1,2,3,4,5</mark>

产生了想要的正确输出(每一项乘以 10):

<processedItem>10</processedItem>
<processedItem>20</processedItem>
<processedItem>30</processedItem>
<processedItem>40</processedItem>
<processedItem>50</processedItem>

二。 XSLT 2.0 解决方案:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="mark">
<xsl:for-each select="tokenize(., ',')">
<processedItem>
<xsl:sequence select="10*xs:integer(.)"/>
</processedItem>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

关于xml - xslt 1.0 中的拆分函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7425071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com