3

XInclude / XPointer 这里令人沮丧的问题。

目的是将 XML 格式的价格条目列表中的条目包含到另一个文档中。我有一个包含如下价格列表的文档:

 <?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE preise [
   <!ELEMENT preise (preis+)>
   <!ELEMENT preis (#PCDATA)>
   <!ATTLIST preis id ID #REQUIRED>
 ]>
 <preise>
   <preis id="a0">./.</preis>
 <preis id='foo100'>136,10</preis>
 <preis id='foo101'>163,32</preis>
 </preise>

以下包含失败

 <xi:include href="../listen/preise.xml#xpointer(/preise/preis[@id='foo100']/text())" />

 element include: XInclude error : failed build URL

现在,如果我将价目表中 id 的格式更改为完全数字

 <?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE preise [
   <!ELEMENT preise (preis+)>
   <!ELEMENT preis (#PCDATA)>
   <!ATTLIST preis id ID #REQUIRED>
 ]>
 <preise>
   <preis id="a0">./.</preis>
 <preis id='100'>136,10</preis>
 <preis id='101'>163,32</preis>
 </preise>

并使用不带撇号的包含

 <xi:include href="../listen/preise.xml#xpointer(/preise/preis[@id=100]/text())" />

突然一切正常。所以这个问题似乎与撇号有关,但我该如何解决呢?

另外,这是我的 xmllint 版本信息:

 xmllint: using libxml version 20706
    compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib
4

1 回答 1

4

来自XInclude W3C 规范

xi:include 元素具有以下属性:

链接

一个值,在执行了适当的转义(参见 4.1.1 href 属性值的转义)之后,会导致一个 URI 引用或一个 IRI 引用指定要包含的资源的位置。href 属性是可选的;该属性的缺失与指定 href="" 相同,即引用的是同一个文档。如果 parse="xml" 时没有 href 属性,则必须存在 xpointer 属性。不得使用片段标识符;他们的出现是一个致命的错误。导致语法上无效的 URI 或 IRI 的值应报告为致命错误,但某些实现可能会发现将这种情况与资源错误区分开来是不切实际的。

因此,“不能使用片段标识符;它们的出现是一个致命错误。

解决方案:尝试省略该href属性并使用该xpointer属性。

但是,请注意同一规范中的以下文本

对 [XPointer xpointer() Scheme] 的支持不是完全符合 XInclude 的强制要求。建议作者使用 xpointer() 和其他 XPointer 方案而不是 element() 可能不支持所有符合 XInclude 实现

最后,这是使用 XPointer 片段包含规范的示例:

下面说明了包含另一个 XML 文档的片段的结果。假设文档的基本 URI 是http://www.example.com/JoeSmithQuote.xml

<?xml version='1.0'?>
<price-quote xmlns:xi="http://www.w3.org/2001/XInclude">
  <prepared-for>Joe Smith</prepared-for>
  <good-through>20040930</good-through>
  <xi:include href="price-list.xml" xpointer="w002-description"/>
  <volume>40</volume>
  <xi:include href="price-list.xml" xpointer="element(w002-prices/2)"/>
</price-quote>

price-list.xml 引用了一个 DTD,它将 id 属性声明为类型 ID,并包含:

<?xml version='1.0'?>
<!DOCTYPE price-list SYSTEM "price-list.dtd">
<price-list xml:lang="en-us">
  <item id="w001">
    <description id="w001-description">
      <p>Normal Widget</p>
    </description>
    <prices id="w001-prices">
      <price currency="USD" volume="1+">39.95</price>
      <price currency="USD" volume="10+">34.95</price>
      <price currency="USD" volume="100+">29.95</price>
    </prices>
  </item>
  <item id="w002">
    <description id="w002-description">
      <p>Super-sized widget with bells <i>and</i> whistles.</p>
    </description>
    <prices id="w002-prices">
      <price currency="USD" volume="1+">59.95</price>
      <price currency="USD" volume="10+">54.95</price>
      <price currency="USD" volume="100+">49.95</price>
    </prices>
  </item>
</price-list>

解决此文档中包含的信息集与以下文档的信息集相同(包含历史记录和语言属性除外):

<?xml version='1.0'?>
<price-quote xmlns:xi="http://www.w3.org/2001/XInclude">
  <prepared-for>Joe Smith</prepared-for>
  <good-through>20040930</good-through>
  <description id="w002-description" xml:lang="en-us"
               xml:base="http://www.example.com/price-list.xml">
    <p>Super-sized widget with bells <i>and</i> whistles.</p>
  </description>
  <volume>40</volume>
  <price currency="USD" volume="10+" xml:lang="en-us"
         xml:base="http://www.example.com/price-list.xml">54.95</price>
</price-quote>
于 2010-11-19T21:25:42.070 回答