English 中文(简体)
xml and namespace anomaly
原标题:

What is the difference between the following pieces of xml?

The reason I ask is that when I submit the xml to a BPEL process the first and second one work but the last one does not, what is going on?

<!-- imported namespace referenced with prefix -->
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:fxd="http://aaa.yy.zz/Foo">
     <soap:Body>
         <fxd:GSR>
             <aaa>
                 <a>1000000</a>
                 <c>UUU</c>
                 <cp>ZZ</cp>
             </aaa>
             <bbb>
                 <cc>CCC</cc>
                 <v>110005632501</v>
             </bbb>
             <adate>2009-11-04T07:14:44.5814828+02:00</adate>
             <bdate>2009-11-04T07:14:44.5814828+02:00</bdate>
             <m>NNNN</m>
             <p>SSSS</p>
             <r>LLLL</r>
         </fxd:GSR>
     </soap:Body>
    </soap:Envelope>        

<!-- inline imported namespace referenced with a prefix-->
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <soap:Body>
         <fxd:GSR xmlns:fxd="http://aaa.yy.zz/Foo">
             <aaa>
                 <a>1000000</a>
                 <c>UUU</c>
                 <cp>ZZ</cp>
             </aaa>
             <bbb>
                 <cc>CCC</cc>
                 <v>110005632501</v>
             </bbb>
             <adate>2009-11-04T07:14:44.5814828+02:00</adate>
             <bdate>2009-11-04T07:14:44.5814828+02:00</bdate>
             <m>NNNN</m>
             <p>SSSS</p>
             <r>LLLL</r>
         </fxd:GSR>
     </soap:Body>
</soap:Envelope>


<!-- inline namespace -->
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <soap:Body>
           <GSR xmlns="http://aaa.yy.zz/Foo">
               <aaa>
                   <a>1000000</a>
                   <c>UUU</c>
                   <cp>ZZ</cp>
               </aaa>
               <bbb>
                   <cc>CCC</cc>
                   <v>110005632501</v>
               </bbb>
               <adate>2009-11-04T07:14:44.5814828+02:00</adate>
               <bdate>2009-11-04T07:14:44.5814828+02:00</bdate>
               <m>NNNN</m>
               <p>SSSS</p>
               <r>LLLL</r>
           </GSR>
       </soap:Body>
</soap:Envelope>

My intuition says they are equivalent pieces of xml, especially considering they come from the same wsdl. They are parsed successfully but the namespaces of the elements are not what they should be.

最佳回答

They are not equal. That is, example 1 and 2 are equal, but 3 is not.

Look at <fxd:GSR> in contrast to <GSR>. You see, that the first one is prefixed. Now, if you define a namespace xmlns:fxd="", all equally prefixed elements are set in this namespace. All others (including elements without any prefix at all) are not in this namespace.

Then, in your third example, you define a namespace for all unprefixed elements. This leads to the fact, that the unprefixed children of GSR are suddenly in the same namespace as their ancestor, not in the nullnamespace they were before in 1 and 2.

Edit: Just a small clarification:

xmlns:fxd="http://aaa.yy.zz/Foo"

sets the namespace to "http://aaa.yy.zz/Foo" for all elements that start with fxd: .

xmlns="http://aaa.yy.zz/Foo"

sets the namespace to "http://aaa.yy.zz/Foo" for all elements that have no colon in their name (= they are not prefixed).

If you want 1 and 2 to behave like 3, just add

xmlns="http://aaa.yy.zz/Foo"

somewhere before the first unprefixed element occurs. If you want it the other way round, you would have to prefix all elements you want to be in no namespace with, say, bar: and add this somewhere:

xmlns:bar=""

thus explicitly setting them in the null namespace (as they are in the first two examples).

问题回答

in your last sample, wouldn t it put all elements enclosed in the GSR element in the fxd namespace ? in the first 2 samples, those elements enclosed in the GSR element are not in the fxd namespace.

so, i would say the last sample is different from the first 2.





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签