xsltproc(osx)“不是样式表,编译错误”。但xml& xsl都在在线工具上工作

时间:2012-01-13 03:45:10

标签: xml xslt

设法让我的xsl在http://www.xmlper.com/工作(来自其他stackoverflow评论的好工具推荐)。

但当我运行相同的(现在高度剥离)xml&在我的mac os x命令行上的xsl我得到"编译错误,Document不是样式表"

是版本问题(下面是xsltproc的输出)吗?我正在运行最新的mac(10.7)和最新的xcode 4.2(以确保xml lib等)? 我找不到任何关于xsltproc不支持xslt v2.0的内容(我尝试过设置为v1.0 - 没有区别) 我确实尝试过下载其他xslt处理器(例如saxon),但是进入了旧版本的java版本,无法让程序运行。

希望有人遇到过这个并解决了它! 谢谢

输入xml文件

<?xml version="1.0" encoding="utf-8"?>
<resultsfile>
  <planset>
    <voicemail_divert>
      <cost_over_plan>0</cost_over_plan>
      <this_txn_absolutely_cost_subtotal>0</this_txn_absolutely_cost_subtotal>
    </voicemail_divert>
  </planset>
</resultsfile>

XSL

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml"/>
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
  <xsl:template match="cost_over_plan">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
    <extracost_overplan_aspartoftotal>0</extracost_overplan_aspartoftotal>
    <extraqty_overplan_aspartoftotal>0</extraqty_overplan_aspartoftotal>
</xsl:template>
</xsl:stylesheet>

错误

$ xsltproc --debug --novalid --nonet f.xml add_extraqtyandcostfields.xsl 
compilation error: file f.xml line 2 element resultsfile
 xsltParseStylesheetProcess : document is not a stylesheet

版本信息

$ xsltproc -version
Using libxml 20703, libxslt 10124 and libexslt 813
xsltproc was compiled against libxml 20703, libxslt 10124 and libexslt 813
libxslt 10124 was compiled against libxml 20703
libexslt 813 was compiled against libxml 20703

2 个答案:

答案 0 :(得分:4)

manpage中所述,样式表必须位于xsltproc参数中的XML文件之前:

$ xsltproc --debug --novalid --nonet add_extraqtyandcostfields.xsl f.xml

应该有用。

此外,您的xsltproc似乎不支持XSLT 2.0。幸运的是,您的样式表似乎不需要new XSLT 2.0 features。您只需将<xsl:stylesheet version="2.0"替换为<xsl:stylesheet version="1.0"即可解决问题。

答案 1 :(得分:1)

您进行了XSLT 2.0转换,但使用的XSLT处理器(libxslt)仅支持XSLT 1.0

一种可能的解决方案是使用XSLT 1.0转换。这可能需要从轻微修改到重大修改,具体取决于当前转换中使用的XSLT 2.0功能。

我在提供的XSLT代码中看不到单个XSLT 2.0功能 - 因此只需将version artttibute的值从2.0更改为1.0可以消除观察到的错误。