1

There is my problem.

I have an XML file using references and including other XML file

XML file (parsed by XSL) :

<?xml version="1.0" encoding="utf-8"?>
<!ENTITY % references SYSTEM "myref.ref">
<test>
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="file2.XML"/>
</test>

file2.XML :

<?xml version="1.0" encoding="utf-8"?>
<action>&testref;</action>

myref.ref :

<!ENTITY  testref 'cool ref there'>

XSL :

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:apply-templates select="//test"/>
</xsl:template>

<xsl:template match="test">
    <xsl:for-each select="document('file2.XML')//action">
    [...]
</xsl:template>

Then I have an XSL that use document() function to open file2.XML which includes references (referenced in myref.ref).

So I get an error saying that The entity "refentity" was referenced, but not declared, when I use document('file2.XML').

How can my myref.ref also be used when I'm opening other XML with document() ?

Thanks.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
Rems
  • 55
  • 9
  • Which XSLT processor do you use, and (more importantly) which underlying XML parser do you use? Non-validating parsers like expat might not parse external entities, and even with validating parsers such features are sometimes disabled by default for security reasons. – Martin Honnen Jun 05 '13 at 10:35
  • I've try with both Xalan and Saxon XSLT processor and I have the same error. I don't use any underlying XML parser in the transformation process, the problem should be is in the XSL. The external XML file is correctly read. If I don't have any references in it, it works fine. – Rems Jun 05 '13 at 12:01
  • Counld you share your XML file and XSLT file with us. – Navin Rawat Jun 05 '13 at 12:08
  • Well your XSLT processor does not parse the XML itself, it uses an XML parser. If you use Saxon (9.5?), have you tried to play with the command line options http://www.saxonica.com/documentation/html/using-xsl/commandline.html like `-dtd:(on|off|recover)`? And your sample is confusing, it has `DOCTYPE doctype` meaning the root element name of the XML should be `doctype`, yet you have `xi:include`. Can you post minimal but complete samples of all files to reproduce the problem? I am currently not sure in which files you have the DOCTYPE and in which the error. – Martin Honnen Jun 05 '13 at 12:14
  • @NavinRawat The xml is already there, the XSLT use document() function on file2.XML, file2.XML is just a reference within a block. In the reference file there is only one reference. The error is exactly what I wrote in my post. – Rems Jun 05 '13 at 12:16
  • @MartinHonnen in fact I use Xalan (or Saxon) in Jedit, I don't know which XML parser it uses. Forget about the DOCTYPE, I deleted it. – Rems Jun 05 '13 at 12:19
  • @MartinHonnen I added the file content, hope it can help you. – Rems Jun 05 '13 at 12:38
  • Thanks for providing more details: as far as I see it now you have `file2.XML` referencing an entity with `&testref;` which is not declared in that file. And your XSLT simply does `document('file2.XML')` so it tries to load that file and of course the XML parser fails to parse it because inside of that file the entity is referenced but not defined. – Martin Honnen Jun 05 '13 at 12:54
  • @MartinHonnen Exactly ! So my question is about the possibility to use the references file with the files parsed by document(). Or at least is there an other way to do that, maybe without the document() function ? – Rems Jun 05 '13 at 12:58

1 Answers1

1

The file you describe as "XML file (parsed by XSL)" is not XML; it would be XML if the entity declaration were wrapped in a document-type declaration, but right now it's not. If your XSLT processor is not complaining, I assume you are handing it not what you show but something more like

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE test [
<!ENTITY % references SYSTEM "myref.ref">
]>
<test>
  <xi:include 
    xmlns:xi="http://www.w3.org/2001/XInclude" 
    href="file2.XML"/>
</test>

The XInclude specification specifies that by default included resources are parsed as if they had MIME type application/xml; it explicitly leaves the performance or non-performance of DTD processing unconstrained. If an XInclude processor does perform DTD processing on it, I would expect it to use the DTD of the XInclude target, not the DTD of the including XML document. That is, I would be surprised if any XInclude processor actually did what you hope for, given the input shown. The copy of libxml on my system just raises the error you mention, complaining that the entity 'testref' is not defined.

On the other hand, I have to admit that it would be hard to argue that what you want is clearly incompatible with the XInclude spec. The spec, after all, explicitly says that "Particulars of whether DTD or XML schema validation are performed ... are not constrained by this specification." Given that statement, it would be hard to argue that the XInclude spec requires that any DTD processing of the included resource be performed using the included resource's DTD and not the including resource's DTD. DTD processing is unconstrained by the XInclude spec; I don't see a way to prove that it's wrong to want the included resource to be parsed as if it were an external entity. By the same token, of course, xmllint is clearly within its rights as a conforming processor when it complains about the undeclared entity.

Since the design of XInclude is so clearly an attempt to make external entities unnecessary, it would be a very unusual implementation that decided to parse included resources as if they were external entities. (And what would such a processor do if the included resource had its own DTD? Issue an error? Perform fallback processing?) Right or wrong, I doubt you will find any XInclude processors that do what you would like here; I'd re-think my design, if I were you.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65