在 XSLT 中,如何截断数字字符串并在字母前添加 space?
In XSLT, how do I truncate a string of numerals and add space before letters?
我有这样的输入:
<?xml version="1.0" encoding="UTF-8"?>
<Article>
<ProductNr>70001</ProductNr>
<ProductNr>70001A</ProductNr>
<ProductNr>70002I</ProductNr>
<ProductNr>70002II</ProductNr>
<ProductNr>70002.1</ProductNr>
<ProductNr>70002.2</ProductNr>
<ProductNr>70123</ProductNr>
<ProductNr>70125A</ProductNr>
</Article>
我有产品编号,我想将其转换为章节编号。我剪下了下面的 XML 输出。我需要做的是删除 70+ 前缀并在数字和字母之间添加 space 。什么都不做。请帮忙!
<?xml version="1.0" encoding="UTF-8"?>
<Article>
<ChapterNr>1</ChapterNr>
<ChapterNr>1 A</ChapterNr>
<ChapterNr>2 I</ChapterNr>
<ChapterNr>2 II</ChapterNr>
<ChapterNr>2.1</ChapterNr>
<ChapterNr>2.2</ChapterNr>
<ChapterNr>123</ChapterNr>
<ChapterNr>125 A</ChapterNr>
</Article>
在没有正则表达式支持的 XSLT 1.0 中,这很难做到。不过,我相信您可以将其归结为:
XSLT 1.0
<xsl:stylesheet version="1.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="ProductNr">
<ChapterNr>
<xsl:variable name="num-start">
<xsl:choose>
<xsl:when test="starts-with(., '70000')">6</xsl:when>
<xsl:when test="starts-with(., '7000')">5</xsl:when>
<xsl:when test="starts-with(., '700')">4</xsl:when>
<xsl:when test="starts-with(., '70')">3</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="num" select="substring(., $num-start)" />
<xsl:choose>
<xsl:when test="contains($num, '.')">
<xsl:value-of select="$num"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="digits-only" select="translate($num, translate($num, '0123456789', ''), '')" />
<xsl:variable name="count-digits" select="string-length($digits-only)" />
<xsl:value-of select="substring($num, 1, $count-digits)"/>
<xsl:if test="string-length($num) > $count-digits">
<xsl:text> </xsl:text>
<xsl:value-of select="substring($num, $count-digits + 1)"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</ChapterNr>
</xsl:template>
</xsl:stylesheet>
请注意此处所做的假设:
70+ 前缀有一个已知的最大长度("70000"
例子);
前缀后面是一串数字,后面是一串
仅包含字母字符(即字母后不会出现任何数字
个字符)。
如果这些假设中的任何一个不成立,您将不得不使用递归命名模板来确定相应的结果。
我有这样的输入:
<?xml version="1.0" encoding="UTF-8"?>
<Article>
<ProductNr>70001</ProductNr>
<ProductNr>70001A</ProductNr>
<ProductNr>70002I</ProductNr>
<ProductNr>70002II</ProductNr>
<ProductNr>70002.1</ProductNr>
<ProductNr>70002.2</ProductNr>
<ProductNr>70123</ProductNr>
<ProductNr>70125A</ProductNr>
</Article>
我有产品编号,我想将其转换为章节编号。我剪下了下面的 XML 输出。我需要做的是删除 70+ 前缀并在数字和字母之间添加 space 。什么都不做。请帮忙!
<?xml version="1.0" encoding="UTF-8"?>
<Article>
<ChapterNr>1</ChapterNr>
<ChapterNr>1 A</ChapterNr>
<ChapterNr>2 I</ChapterNr>
<ChapterNr>2 II</ChapterNr>
<ChapterNr>2.1</ChapterNr>
<ChapterNr>2.2</ChapterNr>
<ChapterNr>123</ChapterNr>
<ChapterNr>125 A</ChapterNr>
</Article>
在没有正则表达式支持的 XSLT 1.0 中,这很难做到。不过,我相信您可以将其归结为:
XSLT 1.0
<xsl:stylesheet version="1.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="ProductNr">
<ChapterNr>
<xsl:variable name="num-start">
<xsl:choose>
<xsl:when test="starts-with(., '70000')">6</xsl:when>
<xsl:when test="starts-with(., '7000')">5</xsl:when>
<xsl:when test="starts-with(., '700')">4</xsl:when>
<xsl:when test="starts-with(., '70')">3</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="num" select="substring(., $num-start)" />
<xsl:choose>
<xsl:when test="contains($num, '.')">
<xsl:value-of select="$num"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="digits-only" select="translate($num, translate($num, '0123456789', ''), '')" />
<xsl:variable name="count-digits" select="string-length($digits-only)" />
<xsl:value-of select="substring($num, 1, $count-digits)"/>
<xsl:if test="string-length($num) > $count-digits">
<xsl:text> </xsl:text>
<xsl:value-of select="substring($num, $count-digits + 1)"/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</ChapterNr>
</xsl:template>
</xsl:stylesheet>
请注意此处所做的假设:
70+ 前缀有一个已知的最大长度(
"70000"
例子);前缀后面是一串数字,后面是一串 仅包含字母字符(即字母后不会出现任何数字 个字符)。
如果这些假设中的任何一个不成立,您将不得不使用递归命名模板来确定相应的结果。