Biztalk Development Series (16) XML namespace

Source: Internet
Author: User
Tags xslt biztalk

If XML is developed during BizTalk development, such as operating XML messages in custom code or using XPath to operate XML during mapping. You will have the opportunity to encounter XML namespace problems. When using XPath to select a node, you do not know whether to add a namespace prefix or when to add it. To this end, make a sample to verify the effect of the XML namespace in the XML operation process.

Namespace is a mechanism provided by W3C recommendation standards to uniformly name elements and attributes in XML documents. The namespace can be used to clearly identify and combine elements and attributes from different tag vocabulary in XML documents, avoiding conflicts between names. The XML namespace attribute is placed in the start tag of an element and uses the following syntax:

Xmlns: namespace-Prefix = "namespaceuri"

When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace.

Sample description

Create a test XML file. For example, [code1] sets different namespace usage scenarios. Create an XSLT (XML Conversion language) file, for example, [code2]. Use breakpoints in XSLT testing in Visual Studio to query through XPath in the monitoring window. Verify under which nodes require namespaces.

[Code1]

<? XML version = "1.0" encoding = "UTF-8"?>
<Root xmlns: ns0 = "http://quicklearn.cn" xmlns: NS1 = "http://addressbook.quicklearn.cn" xmlns: int32 = "http://person.quicklearn.cn">
<NS1: addressbook id = "1">
<Ns2-person>
<Name> cbcye </Name>
<Mobile> 1, 12345 </mobile>
</NS2.: person>
</NS1: addressbook>
</Root>


[Code2]

<? XML version = "1.0" encoding = "UTF-16"?>
<XSL: stylesheet xmlns: XSL = "http://www.w3.org/1999/XSL/Transform"
Version = "1.0"
Xmlns: Qn = "http://quicklearn.cn "/
Xmlns: ADR = "http://addressbook.quicklearn.cn "/
Xmlns: Per = "http://person.quicklearn.cn "/
>
<XSL: template match = "/">
<XSL: Apply-templates select = "/"/>
</XSL: Template>
</XSL: stylesheet>

 

[Test textures]

 

 Default namespace

The default namespace applies to the elements that declare it (if the element does not have a namespace prefix) and all elements that have a prefix in the element content. If the URI reference in the default namespace declaration is null, elements without a prefix in the declaration range are not considered to exist in any namespace. Note that the default namespace does not apply directly to attributes.

 

Test1 has no namespace

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Addressbook id = "1">
...
</Addressbook>
</Root>

Element: addressbook XPath expression:/root/addressbook
Attribute: Id XPath expression:/root/addressbook/@ ID

Test2 has namespace 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root xmlns = http://quicklearn.cn/>
<Addressbook id = "1">
...
</Addressbook>
</Root>

Element: addressbook XPath expression:/Qn: Root/Qn: addressbook (qN is the prefix declared in the XSLT file)
Attribute: Id XPath expression:/Qn: Root/Qn: addressbook/@ ID (qN is the prefix declared in the XSLT file)

 
Namespace prefix

The namespace prefix provides the namespace prefix of a valid name and must be associated with a namespace URI reference in the namespace declaration. Note that the prefix only acts as a namespace placeholder. Applications should use namespace names instead of prefixes to form names beyond the scope of the contained documents.

Test1 prefix + root node 

<? XML version = "1.0" encoding = "UTF-8"?>
<Ns0: Root xmlns: ns0 = http://quicklearn.cn/>
<Addressbook id = "1">
...
</Addressbook>
</Ns0: root>

Element: addressbook XPath expression:/Qn: Root/addressbook (qN is the prefix declared in the XSLT file)
Attribute: Id XPath expression:/Qn: Root/addressbook/@ ID (qN is the prefix declared in the XSLT file)

 

Test2 prefix + child element 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root xmlns: ns0 = http://quicklearn.cn/>
<Ns0: addressbook id = "1">
...
</Ns0: addressbook>
</Root>

Element: addressbook XPath expression:/root/Qn: addressbook (qN is the prefix declared in the XSLT file)
Attribute: Id XPath expression:/root/Qn: addressbook/@ ID (qN is the prefix declared in the XSLT file)


Multiple namespaces

There are multiple namespaces in an XML document, and the namespace uses the default or both prefix to constrain the element or attribute to exist in the middle namespace.

Test1

<? XML version = "1.0" encoding = "UTF-8"?>
<Root xmlns = http://quicklearn.cn/>
<Addressbook id = "1" xmlns = http://addressbook.quicklearn.cn/>
...
</Addressbook>
</Ns0: root>

Element: addressbook XPath expression:/Qn: Root/ADR: addressbook (qN, ADR is the prefix declared in the XSLT file)
Attribute: Id XPath expression:/Qn: Root/ADR: addressbook/@ ID (qN, ADR is the prefix declared in the XSLT file)

Test2 

<? XML version = "1.0" encoding = "UTF-8"?>
<Root xmlns: ns0 = "http://quicklearn.cn"/xmlns: NS1 = "http://addressbook.quicklearn.cn"/xmlns: int32 = "http://person.quicklearn.cn"/>
<NS1: addressbook id = "1">
<Ns2-person>
<Name> cbcye </Name>
<Mobile> 1, 12345 </mobile>
</NS2.: person>
</NS1: addressbook>
</Root>

Element: Name XPath expression:/root/ADR: addressbook/PER: person/Name
(QN, ADR, PER is the prefix declared in the XSLT file)
Attribute: Id XPath expression:/root/ADR: addressbook/@ ID (qN, ADR is the prefix declared in the XSLT file)

 

Summary

The rules for using XML Namespaces can be summarized as follows:The default namespace constraint is global, and the prefix namespace constraint is local. The local declaration takes precedence over the global one.In addition, if you can see this, you can answer this question by the way: Is there a namespace for the test node in the following XML document? If so, what is the namespace?

 

<Ns0: Root xmlns: ns0 = "http://quicklearn.cn"> <Test> </test> </ns0: root>

 

[Update: 2008.10.21 :40]

If you have installed XML spy. You can directly use the XML spy's XPath evaluation function (Click XML in the menu bar to select the XPath value ). Use an XPATH expression to obtain the namespace of each node or attribute.

 

Xpth expression

// Concat (Local-Name (), ">>>", namespace-Uri () All nodes, including blank characters

// @ */Concat (Local-Name (), ">>>", namespace-Uri () All attribute nodes

// */Concat (Local-Name (), ">>>", namespace-Uri () All nodes, excluding blank characters

Biztalk Development Series (16) XML namespace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.