我在样式表中使用XSLT 2.0元素analyze-string,将XML转换为HTML;具体来说,我用它来将化学公式中的下标的字符串编码转换为HTML下标 . 因此,结果是一个字符串,用于嵌入标记的p或td元素 .
转换应该产生像H2O这样的输出但实际上在HTML中插入一个换行符:
H
<sub>2</sub>O
并且这个中断被浏览器(正确地)解释为空格:
H 2O
这是丑陋的 .
有没有办法删除换行符?我已经尝试将整个analyze-string元素放在一行上,但这不起作用 .
输入就像是
<OrdinaryStructralFormula>H$_2$O</OrdinaryStructuralFormula>
对于一个简单的案例和
<OrdinaryStructralFormula>C$_2$OH$_5$$^-</OrdinaryStructuralFormula>
对于一个更复杂的 . 请注意,下标模式在一般情况下可以匹配多次,可以在字符串的中间或末尾 . 该模式还必须匹配并消除任何带电符号:第二个示例末尾的$ ^ - 位 .
XSLT处理器是Saxon 9.4,后面是XSLT模板 .
<xsl:template name="formula">
<xsl:param name="formula"/>
<xsl:if test="$formula">
<xsl:variable name="f" select="translate($formula, '$', '')"/>
<xsl:analyze-string select="$f" regex="(_)(\d+)|(\^)\d*\+|(\^)\d*\-">
<xsl:matching-substring>
<xsl:if test="regex-group(1)='_'">
<sub><xsl:value-of select="regex-group(2)"/></sub>
</xsl:if>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:if>
</xsl:template>
1 回答
I cannot reproduce the reported result .
This transformation (这是你应该给我们的,但你只提供了一个模板):
when applied on the following XML document with Saxon 9.1.05:
produces the wanted, correct result :
When the same transformation is applied on the second XML document :
Again the wanted correct result is produced:
Do note :我使用另外两个XSLT 2.0处理器运行相同的转换:XQSharp(XMLPrime)和AltovaXML(XML-SPY),并获得完全相同的正确结果 .