xslt - 转换为 SVG 文件时的 XSL 命名空间问题

标签 xslt svg namespaces

我正在从一些 alto xml 文件制作一个简单的 SVG 文件。除了烦人的命名空间问题外,我让它工作。

源 xml(我不应该更改)的标题是

    <alto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://www.loc.gov/standards/alto/ns-v2#" 
    xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v2# http://www.loc.gov/standards/alto/alto-v2.0.xsd" 
    xmlns:xlink="http://www.w3.org/1999/xlink"
    >

和我的 xsl 的标题(如果我们能解决这个问题,我很乐意更改)
<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/2000/svg"
   >

这两件事结合在一起造成了撒克逊错误:

严重程度:警告
描述:SXXP0005:源文档在命名空间 http://www.loc.gov/standards/alto/ns-v2# 中, 但所有模板规则都匹配没有命名空间的元素
网址:http://www.saxonica.com/html/documentation/javadoc/net/sf/saxon/trans/SaxonErrorCode.html#SXXP0005

如果我删除行 xmlns="http://www.loc.gov/standards/alto/ns-v2#"从源头来看,我的转换工作得很好......但我不想为了让这个脚本正常工作而修改和取消修改数百个这些文件。所以我真的需要一个改变来修复 xsl。我已经尝试将 alto 行添加到 xsl 表中,但这只会给出“已经有一个命名空间”错误。

最佳答案

正如警告消息所暗示的那样,源 XML 中的所有元素都在 http://www.loc.gov/standards/alto/ns-v2# 中。命名空间,因此您需要将此命名空间绑定(bind)到样式表中的前缀:

<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:a="http://www.loc.gov/standards/alto/ns-v2#"
   >

然后在模板规则和 XPath 表达式中使用前缀,例如而不是 /alto/foo/bar你会用 /a:alto/a:foo/a:bar .在 XSLT 1.0 样式表中,这是匹配命名空间中元素的唯一方法,但是既然您说您使用的是 Saxon,您可以将样式表切换到 XSLT 2.0 并使用 xpath-default-namespace避免必须在任何地方为所有内容添加前缀:
<xsl:stylesheet version="2.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/2000/svg"
   xpath-default-namespace="http://www.loc.gov/standards/alto/ns-v2#"
   >

有了这个,路径像 /alto将匹配相关命名空间中的元素。

关于xslt - 转换为 SVG 文件时的 XSL 命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105728/

相关文章:

XSLT/XPath : No upper-case function in MSXML 4. 0?

xslt - XSL 将节点集存储在变量中

javascript - 有没有办法为文本而不是其他 svg 元素保持纵横比固定?

html - 外部 CSS 形状 - Firefox 浏览器支持

actionscript-3 - 闪存命名空间错误 1004 : Namespace was not found or is not a compile-time constant

php - XSLT/Xpath : Selecting a preceding comment

regex - 如何在XSLT 1.0中使用正则表达式?

javascript - 当图表图像不适合内部时,在栏外显示图表图像

ruby - 从模块中访问类的包含命名空间

xpath - 有什么方法可以从 XML 文件中去除命名空间垃圾?