问题描述
我有以下HTML文档,其中包含MathML命名间隔的元素:
<html>
<head>
<title>Equations</title>
</head>
<body>
<p>
<!-- MathML element -->
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<mi>m</mi>
</mrow>
</math>
</p>
</body>
</html>
我有以下XSLT,它可以在顶级(默认)名称和MathML名称空间中匹配/修改/创建内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:m="http://www.w3.org/1998/Math/MathML"
exclude-result-prefixes="xs m">
<!-- baseline identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- match and create elements in top-level (default) namespace -->
<xsl:template match="body">
<xsl:copy>
<xsl:attribute name="MATCHED" select="1"/>
<div CREATED="1">
<xsl:apply-templates select="p"/>
</div>
</xsl:copy>
</xsl:template>
<!-- match and create elements in nested namespace -->
<xsl:template match="m:mrow">
<xsl:copy>
<xsl:attribute name="MATCHED" select="1"/>
<m:mfenced CREATED="1">
<xsl:apply-templates select="m:mi"/>
</m:mfenced>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我指定m:
命名空间前缀应通过exclude-result-prefixes
禁止显示,但它们仍会出现在输出中新创建的MathML元素上(请注意,模板创建的{{1} }元素):
mfenced
如何抑制这些?
解决方法
我使用的解决方案是在每个处理嵌套命名空间内容的<html>
<head>
<title>Equations</title>
</head>
<body MATCHED="1">
<div CREATED="1">
<p>
<!-- MathML element -->
<math xmlns="http://www.w3.org/1998/Math/MathML">
<mrow MATCHED="1">
<m:mfenced xmlns:m="http://www.w3.org/1998/Math/MathML" CREATED="1">
<mi>m</mi>
</m:mfenced>
</mrow>
</math>
</p>
</div>
</body>
</html>
元素上定义一个本地命名空间。模板中的所有匹配/测试表达式仍需要显式的名称空间引用。
在上面的样式表中,两个更改是
<xsl:template>
和
<xsl:template match="m:mrow" xmlns="http://www.w3.org/1998/Math/MathML">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ added
通过这些更改,我们得到的输出与输入一样干净:
<mfenced CREATED="1">
^--- explicit namespace removed
...
</mfenced>
^--- explicit namespace removed
感谢Martin,Wendell,Liam和David(在Mulberry XSLT邮件列表中)和Radu(在Syncro Soft)帮助我了解命名空间!