简体   繁体   English

如何使用带有 xslt 的撒克逊文件将 output 指定为 xml 文件?

[英]How to specify output to an xml file using saxon with xslt?

How can I get the output as xml ?我怎样才能得到output作为xml

nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ java -jar /usr/share/java/saxon.jar -o outputfile.xml  note.xml note.xsl 
nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ cat outputfile.xml 
<?xml version="1.0" encoding="UTF-8"?>
Tove
Jani
Reminder
Don't forget me this weekend!
nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ cat note.xml 
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ cat note.xsl 
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output media-type="text/html"/>
  <xsl:mode on-no-match="shallow-copy" />
</xsl:stylesheet>



nicholas@mordor:~/xml$ 

Specifying an output file looks to be insufficient.指定 output 文件看起来是不够的。 Perhaps the xsl file is incorrect for generating xml ?也许xsl文件对于生成xml不正确?

From the comments, it seems you are using the very ancient Saxon6 product, which only supports XSLT 1.0.从评论看来,您使用的是非常古老的 Saxon6 产品,仅支持 XSLT 1.0。 More recent versions (the current is 10.3) implement XSLT 3.0.更新的版本(当前是 10.3)实现了 XSLT 3.0。

When your stylesheet specifies version="3.0" and your XSLT processor only supports 1.0, then it runs in "forwards compatibility mode".当您的样式表指定 version="3.0" 并且您的 XSLT 处理器仅支持 1.0 时,它会以“向前兼容模式”运行。 In this mode, elements and attributes that weren't defined in the 1.0 specification are ignored.在此模式下,1.0 规范中未定义的元素和属性将被忽略。 One such element is xsl:mode , so your stylesheet runs as if the xsl:mode declaration were not there.一个这样的元素是xsl:mode ,因此您的样式表就像xsl:mode声明不存在一样运行。 This means that instead of shallow-copy, you get the default "no match" template behaviour, which outputs the text nodes of the source document and ignores the element nodes.这意味着您将获得默认的“不匹配”模板行为,而不是浅拷贝,它输出源文档的文本节点并忽略元素节点。

output as expected: output 如预期:

nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ rm outputfile.xml 
nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ java -jar /home/nicholas/saxon/saxon-he-10.3.jar -o:outputfile.xml  note.xml note.xsl 
nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ cat outputfile.xml 
<?xml version="1.0" encoding="UTF-8"?><note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ 
nicholas@mordor:~/xml$ cat note.xsl
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output media-type="text/html"/>
  <xsl:mode on-no-match="shallow-copy" />
</xsl:stylesheet>



nicholas@mordor:~/xml$ 

Just thought I might be able to use the system libraries.只是想我也许可以使用系统库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM