|
Subject:
|
preserving entity references using DOM, JAXP
|
|
Posted By:
|
srivalli9
|
Post Date:
|
11/14/2003 4:28:52 PM
|
Hi, I have an XML file, an element of which refers to a general entity. I'm using DOm parser and JAXP API to parse it and form a resulting XML file. I want the entity reference to be preserved in the resulting XML file which is currently NOT. ---------- DTD..... ---------- <!ENTITY x "Jones"> <!ELEMENT message (approved, signature )> <!ELEMENT approved EMPTY> <!ATTLIST approved flag ( true | false ) "false"> <!ELEMENT signature ( #PCDATA )>
----------- sample.XML...... ----------- <?xml version="1.0" ?> <!DOCTYPE message SYSTEM "sample.dtd"> <message> <approved flag="true"/> <signature>Chairperson &x; PhD</signature> </message>
----------- sampleoutput.xml ----------- <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE message SYSTEM "sample.dtd"> <message> <approved flag="true"/> <signature>Chairperson Jones PhD</signature> </message> <....>
I DO NOT want Jones. Instead, I want it as &x; That is, I want to have the exact same XML output as the SOURCE.
------------------------- My JAVA code follows.... ------------------------- DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); factory.setExpandEntityReferences(true); factory.setValidating(true); factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder=factory.newDocumentBuilder(); builder.setErrorHandler(new MyErrorHandler()); document=builder.parse(new File("C:/sample.xml"));
TransformerFactory tf=TransformerFactory.newInstance(); Transformer t=tf.newTransformer(); t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
FileWriter fileOut = new FileWriter("sampleoutput.xml"); t.transform(new DOMSource(document), new StreamResult(fileOut)); --------------------------------------
sample.xml -- source XML sampleoutput.xml -- output XML sample.dtd -- DTD
Could anyone please figure out how to preserve entity references? Any help in this regard is grately appreciated.
- Srivalli.
|
|
Reply By:
|
armmarti
|
Reply Date:
|
11/15/2003 2:59:39 AM
|
Strange... if you declared "x" as an entity, why do you want to output it as it's represented as entity reference? Before starting the transformation process, the XSLT processor resolves all entity references (also makes some other preparing activities) and then creates internal tree. From that point the processor has no control over the original XML doc, so you have to care about this issue yourself. The simplest solution(though not stable and "dangerous") is to use disable-output-escaping attribute of xsl:value-of. First of all, change the XML source to
<?xml version="1.0" ?>
<!DOCTYPE message SYSTEM "sample.dtd">
<message>
<approved flag="true"/>
<signature>Chairperson <![CDATA[&x;]]> PhD</signature>
</message>
then this stylesheet will output what you want:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="signature">
<xsl:copy>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
So, to do strange things, the way also must be strange!
Regards, Armen
|
|
Reply By:
|
srivalli9
|
Reply Date:
|
11/17/2003 10:00:32 AM
|
Thanks Armen,
Your code worked very well. But my team were reluctant to having CData sections.
may be I shall just leave the entities as they are and let them get expanded.
Thanks again for your help. -Srivalli.
|