XML 和 XSL 在使用 saxon 时带有不需要的命名空间
XML and XSL with unwanted namespace when using saxon
我在 xsl 样式表中使用了 exclude-result-prefixes="ae"。然后命名空间也出现在转换后的 XML 文件中。我正在使用 saxon
解析器。请在下方找到我的 MWE:
我的 XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<ArticleInfo Language="En" ContainsESM="No" OutputMedium="All">
<ArticleID>034</ArticleID>
<ArticleJID>BMCL</ArticleJID>
<ArticleDOI>10.1000/j.asdf.2015.02.034</ArticleDOI>
<ArticleTitle>Sample Article Title with ― unicode value</ArticleTitle>
<Para>Sample Paragraph text here</Para>
</ArticleInfo>
我的 XSL 文件是:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ae="www.ams.org" exclude-result-prefixes="ae" version="3.0">
<xsl:output omit-xml-declaration="no" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">
<!DOCTYPE article PUBLIC "-//AMS//DTD journal article//EN//XML" "art.dtd">
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="ElsDoi" select="/ArticleInfo/ArticleDOI"/>
<xsl:template match="ArticleInfo">
<ae:doi><xsl:value-of select="$ElsDoi"/></ae:doi>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Para">
<xsl:element name="ae:para">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我正在获取输出 XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//AMS//DTD journal article//EN//XML" "art.dtd">
<ae:doi xmlns:ae="www.ams.org">10.1000/j.asdf.2015.02.034</ae:doi>
<ArticleID>034</ArticleID>
<ArticleJID>BMCL</ArticleJID>
<ArticleDOI>10.1000/j.asdf.2015.02.034</ArticleDOI>
<ArticleTitle>Sample Article Title with ― unicode value</ArticleTitle>
<ae:para xmlns:ae="www.ams.org">Sample Paragraph text here</ae:para>
预期输出 XML 文件为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//AMS//DTD journal article//EN//XML" "art.dtd">
<ae:doi>10.1000/j.asdf.2015.02.034</ae:doi>
<ArticleID>034</ArticleID>
<ArticleJID>BMCL</ArticleJID>
<ArticleDOI>10.1000/j.asdf.2015.02.034</ArticleDOI>
<ArticleTitle>Sample Article Title with ― unicode value</ArticleTitle>
<ae:para>Sample Paragraph text here</ae:para>
请注意不需要的 xmlns:ae="www.ams.org"
出现在输出 XML 文件和标题中 ―
被转换为 unicode
符号。如何避免这种情况。
使用 <xsl:element name="ae:para">
时,您将在绑定到前缀 ae
的命名空间中明确创建一个元素,因此不要指望 exclude-result-prefixes 会排除该命名空间,因为它仅用于避免未使用命名空间的命名空间声明。节点名称中使用的名称空间不能用 exclude-result-prefixes
排除,否则结果将不是格式良好的名称空间 XML.
我在 xsl 样式表中使用了 exclude-result-prefixes="ae"。然后命名空间也出现在转换后的 XML 文件中。我正在使用 saxon
解析器。请在下方找到我的 MWE:
我的 XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<ArticleInfo Language="En" ContainsESM="No" OutputMedium="All">
<ArticleID>034</ArticleID>
<ArticleJID>BMCL</ArticleJID>
<ArticleDOI>10.1000/j.asdf.2015.02.034</ArticleDOI>
<ArticleTitle>Sample Article Title with ― unicode value</ArticleTitle>
<Para>Sample Paragraph text here</Para>
</ArticleInfo>
我的 XSL 文件是:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ae="www.ams.org" exclude-result-prefixes="ae" version="3.0">
<xsl:output omit-xml-declaration="no" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">
<!DOCTYPE article PUBLIC "-//AMS//DTD journal article//EN//XML" "art.dtd">
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="ElsDoi" select="/ArticleInfo/ArticleDOI"/>
<xsl:template match="ArticleInfo">
<ae:doi><xsl:value-of select="$ElsDoi"/></ae:doi>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Para">
<xsl:element name="ae:para">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我正在获取输出 XML 文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//AMS//DTD journal article//EN//XML" "art.dtd">
<ae:doi xmlns:ae="www.ams.org">10.1000/j.asdf.2015.02.034</ae:doi>
<ArticleID>034</ArticleID>
<ArticleJID>BMCL</ArticleJID>
<ArticleDOI>10.1000/j.asdf.2015.02.034</ArticleDOI>
<ArticleTitle>Sample Article Title with ― unicode value</ArticleTitle>
<ae:para xmlns:ae="www.ams.org">Sample Paragraph text here</ae:para>
预期输出 XML 文件为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//AMS//DTD journal article//EN//XML" "art.dtd">
<ae:doi>10.1000/j.asdf.2015.02.034</ae:doi>
<ArticleID>034</ArticleID>
<ArticleJID>BMCL</ArticleJID>
<ArticleDOI>10.1000/j.asdf.2015.02.034</ArticleDOI>
<ArticleTitle>Sample Article Title with ― unicode value</ArticleTitle>
<ae:para>Sample Paragraph text here</ae:para>
请注意不需要的 xmlns:ae="www.ams.org"
出现在输出 XML 文件和标题中 ―
被转换为 unicode
符号。如何避免这种情况。
使用 <xsl:element name="ae:para">
时,您将在绑定到前缀 ae
的命名空间中明确创建一个元素,因此不要指望 exclude-result-prefixes 会排除该命名空间,因为它仅用于避免未使用命名空间的命名空间声明。节点名称中使用的名称空间不能用 exclude-result-prefixes
排除,否则结果将不是格式良好的名称空间 XML.