我的XML源代码如下:
<span class="char-style-override-6">Breast Problems (Female and Male) 511</span>
Run Code Online (Sandbox Code Playgroud)
我有一个模板匹配它
<xsl:template match="span" mode="table">
Run Code Online (Sandbox Code Playgroud)
我现在的困难是在这个模板匹配中,我需要创建两个标签,第一个将包含字符串"Breast Problems(Female and Male)",而第二个只包含页码"511".
我只是不知道如何做这个子串分割,以区分文本和数值.
XSLT 2.0 解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<output>
<xsl:apply-templates mode="table"/>
</output>
</xsl:template>
<xsl:template match="span" mode="table">
<xsl:variable name="split" select="replace(., '.*\s(\d+)$', '$1')"/>
<string><xsl:value-of select="normalize-space(substring-before(., $split))"/></string>
<number><xsl:value-of select="$split" /></number>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用于
<?xml version="1.0" encoding="UTF-8"?>
<root>
<span class="char-style-override-6">Breast Problems (Female and Male) 511</span>
</root>
Run Code Online (Sandbox Code Playgroud)
给出
<?xml version="1.0" encoding="UTF-8"?>
<output>
<string>Breast Problems (Female and Male)</string>
<number>511</number>
</output>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6165 次 |
| 最近记录: |