Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top