使用 XSLT 2.0 在输入 XML 中用序列号替换占位符
Replacing placeholders with sequence numbers in input XML using XSLT 2.0
我输入了 XML 如下所示:
<parent>
<data>
<text>1.This is sample text1.</text>
</data>
<data>
<text>This is sample text without number.</text>
</data>
<data>
<text><n>.This is sample text2.</text>
</data>
<data>
<text><n>.This is sample text3.</text>
</data>
<data>
<text><n>.This is sample text4.</text>
</data>
</parent>
所需的输出xml如下:
<parent>
<data>
<text>1.This is sample text1.</text>
</data>
<data>
<text>This is sample text without number.</text>
</data>
<data>
<text>2.This is sample text2.</text>
</data>
<data>
<text>3.This is sample text3.</text>
</data>
<data>
<text>4.This is sample text4.</text>
</data>
</parent>
我需要用 XSLT 2.0 中的解决方案将 替换为从 2 开始的序列号。我尝试使用 for-each with replace 函数和模板以及 replace 函数。但是在模板中我不会得到 position() 值。您能否指导我如何实现这一目标?
I need to replace <n> with sequence numbers starting from 2
怎么样:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text[starts-with(., '<n>')]">
<xsl:variable name="n">
<xsl:number count="text[starts-with(., '<n>')]" level="any"/>
</xsl:variable>
<xsl:copy>
<xsl:value-of select="$n + 1" />
<xsl:value-of select="substring-after(., '<n>')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
没有严格回答问题,但我认为这属于这里。
除了处理字符串,您可能还想考虑处理 XML。
如果您将 XML 更改为:
<parent>
<data>
<text><n />. This is sample text1.</text>
</data>
<data>
<text>This is sample text without number.</text>
</data>
<data>
<text><n />. This is sample text2.</text>
</data>
<data>
<text><n />. This is sample text3.</text>
</data>
<data>
<text><n />. This is sample text4.</text>
</data>
</parent>
(注意:我使用 <n />
而不是 <n>
以便 XML 有效。
那你就可以使用下面的XSL样式表了,在我看来更符合“XML/XSL哲学”。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="n">
<xsl:value-of select="count(preceding::n) + 1" />
</xsl:template>
</xsl:stylesheet>
我输入了 XML 如下所示:
<parent>
<data>
<text>1.This is sample text1.</text>
</data>
<data>
<text>This is sample text without number.</text>
</data>
<data>
<text><n>.This is sample text2.</text>
</data>
<data>
<text><n>.This is sample text3.</text>
</data>
<data>
<text><n>.This is sample text4.</text>
</data>
</parent>
所需的输出xml如下:
<parent>
<data>
<text>1.This is sample text1.</text>
</data>
<data>
<text>This is sample text without number.</text>
</data>
<data>
<text>2.This is sample text2.</text>
</data>
<data>
<text>3.This is sample text3.</text>
</data>
<data>
<text>4.This is sample text4.</text>
</data>
</parent>
我需要用 XSLT 2.0 中的解决方案将
I need to replace <n> with sequence numbers starting from 2
怎么样:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text[starts-with(., '<n>')]">
<xsl:variable name="n">
<xsl:number count="text[starts-with(., '<n>')]" level="any"/>
</xsl:variable>
<xsl:copy>
<xsl:value-of select="$n + 1" />
<xsl:value-of select="substring-after(., '<n>')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
没有严格回答问题,但我认为这属于这里。
除了处理字符串,您可能还想考虑处理 XML。
如果您将 XML 更改为:
<parent>
<data>
<text><n />. This is sample text1.</text>
</data>
<data>
<text>This is sample text without number.</text>
</data>
<data>
<text><n />. This is sample text2.</text>
</data>
<data>
<text><n />. This is sample text3.</text>
</data>
<data>
<text><n />. This is sample text4.</text>
</data>
</parent>
(注意:我使用 <n />
而不是 <n>
以便 XML 有效。
那你就可以使用下面的XSL样式表了,在我看来更符合“XML/XSL哲学”。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="n">
<xsl:value-of select="count(preceding::n) + 1" />
</xsl:template>
</xsl:stylesheet>