我有这个 xml 文件:
<?xml version="1.0" encoding="iso-8859-1"?>
<doclist>
<text attribute="a">This is a <tag1>sentence</tag1> <tag1>with</tag1> a few
<tag1>words</tag1>.</text>
<-- many more text nodes with none, one or several '<tag1>' in it -->
</doclist>
我想得到这个结果:
<?xml version="1.0" encoding="iso-8859-1"?>
<doclist>
<text attribute="a">This is a <tag1>sentence with</tag1> a few <tag1>words</tag1>.
</text>
<-- many more text nodes with none, one or several '<tag1>'s in it -->
</doclist>
我试过用正则表达式来做:
<xsl:template match="text">
<text>
<xsl:apply-templates select="@*"/> <!-- templ. to copy attributes of text -->
<xsl:analyze-string select="."
regex="<tag1>(.+)<tag1><tag1>(.+)</tag1>">
<!-- also tried . instead of < -->
<xsl:matching-substring>
<xsl:for-each select=".">
<tag1>
<xsl:value-of-select="regex-group(1)">
<xsl:text> <xsl:text>
<xsl:value-of-select="regex-group(2)">
</tag1>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:for each select=".">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:non-matching-substring>
</xsl:analyze-string>
</text>
</xsl:template>
但我的输出如下所示:
<?xml version="1.0" encoding="iso-8859-1"?>
<doclist>
<text attribute="a>This is a sentencewitha few words.
</text>
<-- many more text nodes with none, one or several '<tag1>'s in it -->
</doclist>
我的猜测是,没有找到匹配项,因为<tag1>
结果中没有出现 - 但我不明白为什么只有标签包围的单词会丢失它们的空格......我怎样才能正确折叠<tag1>
直接邻居的 s?