You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Pae Choi <pa...@earthlink.net> on 2002/11/19 10:07:25 UTC

[Bug or Feature] CR or CR-LF Disappered -- Xalan v2.3.1 and JARs came with it

I have a home-made utility called, covertDOM2String(), as below. It
takes a DOM and returns it as a string in java.lang.String format. I,
however, noticed that it truncate a CR or CR-LF between the elements,
<?xml version="1.0" ?> and <Patient>.

The code snippet as well as input and output XML Docs are as
follows:

// ########## Input XML #################
<?xml version="1.0" ?>

<Patient>
    <Age>39</Age>
    <Gender>Female</Gender>
    <ContactInfo>
        <PhoneNumber>123-456-7890</PhoneNumber>
        <EmailAddress>patient@somewhere.com</EmailAddress>
    </ContactInfo>
</Patient>


// ########### Code Snippet ##############

    public static String convertDOM2String(Document xmlDocument) {

        String  xmlString = null;

        DOMSource domSource = new DOMSource( xmlDocument );
        StreamResult streamResult = new StreamResult( new
java.io.ByteArrayOutputStream() );
        try {
            Transformer transformer =
TransformerFactory.newInstance().newTransformer();
            transformer.transform( domSource, streamResult );
        }
        catch (Exception ex) {  // javax.xml.transform.TransformerException
            ex.printStackTrace();
        }

        xmlString = streamResult.getOutputStream().toString();

        return xmlString;
    }

// ########## Output XML #################
<?xml version="1.0" ?>
<Patient>
    <Age>39</Age>
    <Gender>Female</Gender>
    <ContactInfo>
        <PhoneNumber>123-456-7890</PhoneNumber>
        <EmailAddress>patient@somewhere.com</EmailAddress>
    </ContactInfo>
</Patient>


The CR or CRLF is removed between <?xml version="1.0" ?> and
<Patient> elements.

Any comments? Thank you.

Regards,


Pae



Re: 1st parameter, "Node context Node" in the XPathAPI's methods

Posted by Pae Choi <pa...@earthlink.net>.
----- Original Message ----- 
From: "Pae Choi" <pa...@earthlink.net>
To: "Pae Choi" <pa...@earthlink.net>; <xa...@xml.apache.org>
Sent: Monday, November 25, 2002 8:45 AM
Subject: Re: 1st parameter, "Node context Node" in the XPathAPI's methods


> Continued from the privious post to be more specific.
> 
> Say we have an XML document as follows:
> 
> <!-- ########### XML Document ########## -->
> <PatientInfo>
>     <Patient id="10099">
>         <DemoInfo>
>             <Age>39</Age>
>             <Gender>Female</Gender>
>         </DemoInfo>
>     </Patient>
> </PatientInfo>
> 
> 
> // ########### Code Snippet ##############
> DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = null;
> Document xmlDoc = null;
> Node node = null;
> NodeIterator nodeIter = null;
> 
> try {
>     DocumentBuilder builder = factory.newDocumentBuilder();
>     xmlDoc = builder.parse(filePath);
> 
>     node = XPathAPI.selectSingleNode(xmlDoc, "//Patient[@id="10099']/*");
>     // The 'node' should reference the <DemoInfo> element. Right?
> 
>     // ********************   QUESTION   ********************
>     // The question is that whether the 'startNode' is referencing the

Oops. The 'startNode' should be 'node'.

>     // <DemoInfo> element or the root element, <PatientInfo>.
>     nodeIter = XPathAPI.selectNodeIterator(node, "./*");
> 
>     // I was thinking that the 'nodeIter' should return the list of
>     // subelements, including the <Age> and <Gender>, but it does not.
>     // That's why I am trying to find out whether the 1st parameter in
>     // all methods of XPathAPI is referencing the root element of DOM
>     // or any node of the DOM passed to it
> }
> catch (Exception ex) {
>     ex.printStackTrace();
> }
> 
> Any comments? Thanks.
> 
> 
> Pae
> 
> 
> 
> 
> > In XPathAPI, all methods has a first parameter, "Node contextNode",
> > and I am clear about that parameter.
> > 
> > Is that always referencing the root element of the DOM? Or, that
> > supposed to be reference any node in the DOM. Thanks.
> > 
> > 
> > Pae
> > 
> > 
> > 
> > 
> 


Re: 1st parameter, "Node context Node" in the XPathAPI's methods

Posted by Pae Choi <pa...@earthlink.net>.
Continued from the privious post to be more specific.

Say we have an XML document as follows:

<!-- ########### XML Document ########## -->
<PatientInfo>
    <Patient id="10099">
        <DemoInfo>
            <Age>39</Age>
            <Gender>Female</Gender>
        </DemoInfo>
    </Patient>
</PatientInfo>


// ########### Code Snippet ##############
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document xmlDoc = null;
Node node = null;
NodeIterator nodeIter = null;

try {
    DocumentBuilder builder = factory.newDocumentBuilder();
    xmlDoc = builder.parse(filePath);

    node = XPathAPI.selectSingleNode(xmlDoc, "//Patient[@id="10099']/*");
    // The 'node' should reference the <DemoInfo> element. Right?

    // ********************   QUESTION   ********************
    // The question is that whether the 'startNode' is referencing the
    // <DemoInfo> element or the root element, <PatientInfo>.
    nodeIter = XPathAPI.selectNodeIterator(node, "./*");

    // I was thinking that the 'nodeIter' should return the list of
    // subelements, including the <Age> and <Gender>, but it does not.
    // That's why I am trying to find out whether the 1st parameter in
    // all methods of XPathAPI is referencing the root element of DOM
    // or any node of the DOM passed to it
}
catch (Exception ex) {
    ex.printStackTrace();
}

Any comments? Thanks.


Pae




> In XPathAPI, all methods has a first parameter, "Node contextNode",
> and I am clear about that parameter.
> 
> Is that always referencing the root element of the DOM? Or, that
> supposed to be reference any node in the DOM. Thanks.
> 
> 
> Pae
> 
> 
> 
> 


Re: 1st parameter, "Node context Node" in the XPathAPI's methods

Posted by Pae Choi <pa...@earthlink.net>.
I was hoping that, but did not work in that way. I have posted
source code, including the XML doc. If you see the Java code and
better to test it to see the result.


Pae


> From the Javadoc:
>         contextNode - The node to start searching from.
> 
> In other words, the note where evaluation of the XPath should start.
> 
> ______________________________________
> Joe Kesselman  / IBM Research


Re: 1st parameter, "Node context Node" in the XPathAPI's methods

Posted by Joseph Kesselman <ke...@us.ibm.com>.
>From the Javadoc:
        contextNode - The node to start searching from.

In other words, the note where evaluation of the XPath should start.

______________________________________
Joe Kesselman  / IBM Research

1st parameter, "Node context Node" in the XPathAPI's methods

Posted by Pae Choi <pa...@earthlink.net>.
In XPathAPI, all methods has a first parameter, "Node contextNode",
and I am clear about that parameter.

Is that always referencing the root element of the DOM? Or, that
supposed to be reference any node in the DOM. Thanks.


Pae





Re: Inserting or appending a set of nodes, i.e., NodeIterator or NodeList, into/to the DOM

Posted by Joseph Kesselman <ke...@us.ibm.com>.
DOM usage questions are indeed offtopic for Xalan. They're slightly more 
on topic for Xerces, but even more on topic for a basic XML usage 
discussion.

Brief answer: No, using only the DOM API there is no simpler way to insert 
the contents of a NodeIterator or NodeList than to walk through the 
iterator or list and insert each node in turn.

______________________________________
Joe Kesselman  / IBM Research

Inserting or appending a set of nodes, i.e., NodeIterator or NodeList, into/to the DOM

Posted by Pae Choi <pa...@earthlink.net>.
First of all, I am not sure if this is related to Xalan. If not,
please discard this message.

Say we have a DOM, i.e, org.w3c.dom.Document, is there
a simple way to insert or append the set of nodes into/to
that DOM.

The representation of those nodes mentioned above will
be either NodeIterator or NodeList returned by XPathAPI's
method, selectNodeIterator() or selectNodeList(), respectively.

For example, we have a DOM as follows:

<patient-info>
    <patient>
    </patient>
</patient-info>

And the set of nodes will be as follows:

        <age>38</age>
        <gender>female</gender>

So afert either inserting or appending the set of nodes, the
result will be as follows:

<patient-info>
    <patient>
        <age>38</age>
        <gender>female</gender>
    </patient>
</patient-info>


Again, is there a way to accomplish this in a simpler manner
instead of parsing the set of nodes and insert or append the
individual nodes one by one. Thanks.

Regards,


Pae



Re: [Bug or Feature] CR or CR-LF Disappered -- Xalan v2.3.1 and JARs came with it

Posted by Joseph Kesselman <ke...@us.ibm.com>.
If XML Signature is looking at anything which isn't part of the XML 
Infoset, then in my opinion it is *WRONG* and needs to be fixed.

A difference which makes no difference should be no difference.

______________________________________
Joe Kesselman  / IBM Research

Re: [Bug or Feature] CR or CR-LF Disappered -- Xalan v2.3.1 and JARs came with it

Posted by Pae Choi <pa...@earthlink.net>.
That may be true when no message encryption and integrity
is not involved. But in the scenario of XML signature/encryption,
say we create a message digest and signature with the XML
document. encrypt the XML document after we convert it to
a string.

When the recipient receve the encrypted message and decrypt
it back to plaintext message(the original XML document). And
guess what happens when the message integrity is involved.
The orignial message was changed, a.k.a., tampered.

Any comments?


Pae

----- Original Message -----
From: "Joseph Kesselman" <ke...@us.ibm.com>
To: <xa...@xml.apache.org>
Sent: Tuesday, November 19, 2002 12:36 PM
Subject: Re: [Bug or Feature] CR or CR-LF Disappered -- Xalan v2.3.1 and
JARs came with it


> This is a Feature. Whitespace outside the root element is officially Not
> Meaningful in XML. It can't be represented in the DOM, and will be
> discarded.
>
> (This also isn't an XSLT or XPath question.)
>
> ______________________________________
> Joe Kesselman  / IBM Research


Re: [Bug or Feature] CR or CR-LF Disappered -- Xalan v2.3.1 and JARs came with it

Posted by Joseph Kesselman <ke...@us.ibm.com>.
This is a Feature. Whitespace outside the root element is officially Not 
Meaningful in XML. It can't be represented in the DOM, and will be 
discarded. 

(This also isn't an XSLT or XPath question.)

______________________________________
Joe Kesselman  / IBM Research