سؤال

I have an input XML that has entities declared in it. It looks something like this:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE doctype PUBLIC "desc" "DTD.dtd" [ 

<!ENTITY SLSD_68115_jpg SYSTEM "68115.jpg" NDATA JPEG>
]>

The DTD.dtd file contains the neccessary notation:

<!NOTATION JPEG SYSTEM "JPG" >

During XSLT transformation I would like to get the URI declared in the entity using the name 'SLSD_68115_jpg' like so:

<xsl:value-of select="unparsed-entity-uri('SLSD_68115_jpg')"/>

So that it would return something like "68115.jpg".

The problem is that it always returns an empty string. There is no way for me to modify the input xml. I understand that this could be a common problem from what I found on the internet, but i haven't found any final conclusions, solutions or alternatives to this problem.

It might be important to note that I had a problem before since I am using a StreamSource and things like systemId had to be set manually, I think this is where the problem might be hidden. It's like the transformator is unable to resolve the entity with given id.

I'm using Xalan, I probably need to provide more details but I'm not sure what to add, I'll answer any questions is there are any.

Any help would be greatly appretiated.

هل كانت مفيدة؟

المحلول

I found out why the "unparsed-entity-uri" was unable to resolve the declared entities. This might be a special case, but I will post this solution so it might save someone else a lot of time.

I'm (very) new to XSLT. The xsl file I got to work with however as a student was pretty extreme with multiple import statements and files containing more than 5K lines of code.

Simply by the time I got to the point where I needed the entities the transformator used a different document that was essentially the sub document of the original one, which is okay, but for example the entity declarations are not passed to the sub document. Therefore there is no way for me to use the entities from that point beyond.

Now like I said im new to XSLT but I think that for example lines like this can cause the problem:

<xsl:apply-templates select="exslt:node-set($nodelist)"/>

Because after this, entity references are no bueno.

If this was trivial then my apologies for waisting your time.

Thanks to everyone none the less!

نصائح أخرى

Instead of a StreamSource, try a SAXSource configured with a validating parser:

SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
spf.setNamespaceAware(true);
XMLReader xmlr = spf.newSAXParser().getXMLReader();

InputSource input = new InputSource(
    new File("/path/to/file.xml").toURI().toString());
// if you already have an InputStream/Reader then do
// input.setByteStream or input.setCharacterStream as appropriate
SAXSource source = new SAXSource(xmlr, input);

Or you can use a DOMSource in the same way

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
File f = new File("/path/to/file.xml");
Document doc = dbf.newDocumentBuilder().parse(f);

DOMSource source = new DOMSource(doc, f.toURI().toString());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top