Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 14th, 2003, 05:28 PM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default preserving entity references using DOM, JAXP

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.

 
Old November 15th, 2003, 03:59 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

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
Code:
<?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:

Code:
<?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
 
Old November 17th, 2003, 11:00 AM
Authorized User
 
Join Date: Nov 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with JAXP (NoClassDefFoundError) Satyajit Sahu XSLT 1 July 13th, 2007 03:53 AM
preserving entity references muki XSLT 6 March 7th, 2006 12:08 PM
Entity References and DOM boemane C# 0 August 24th, 2004 08:21 AM
Difference between Entity and Entity type arshad mahmood C++ Programming 0 May 8th, 2004 12:34 AM
entity references not preserved in XML Output srivalli9 J2EE 0 November 14th, 2003 05:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.