Disclaimer: The following tests are used with jdk1.4.2,xalan7.0 for the following XML document fragment: <ml:mreml xmlns:ml= "Mreml" >
<EnvelopEntity>
<EnvelopID>GUID2006102000002</EnvelopID>
<EnvelopVersion>1.0.1R</EnvelopVersion>
<exchangetime>2006-10-25 13:12:10</exchangetime>
<SendFrom>
<SFID>SOBEY_NEWS</SFID>
<sfdescription>sobey News System </SFDescription>
</SendFrom>
<SendTo>
<STID>DAYANG_MAM</STID>
<stdescription>dayang Media-funded systems </STDescription>
</SendTo>
<Priority>2</Priority>
<EntityInfo>
<EntityID>REQUEST_ID_01</EntityID>
<EntityType>4</EntityType>
</EntityInfo>
<EntityInfo>
<EntityID> program Guid</entityid>
<EntityType>3</EntityType>
</EntityInfo>
<EntityInfo>
<EntityID> Management Information Entity id</entityid>
<EntityType>2</EntityType>
</EntityInfo>
</EnvelopEntity>
</ml:mreml >
For XML documents that contain namespaces, there are two ways to find Envelopid values through XPath: 1. Local-name () by using XPath functions
An XPath expression such as the above lookup can be written as://*[local-name () = ' mreml ']/envelopentity/envelopid/text ()
2. By working with namespaces in Java programs that are defined, if you want XPath expressions to be parsed correctly, you need to establish a mapping relationship between the prefix and the URI in the Java program
public static Node Parsexpath (String expression, Object obj, QName QName)
throws Exception {
Javax.xml.xpath.XPath XPath = javax.xml.xpath.XPathFactory.newInstance (). Newxpath ();
Xpath.setnamespacecontext (Getnamespacecontext ());
if (qname.equals (Xpathconstants.node))
Return (NODE) xpath.evaluate (expression, obj, QName);
return null;
}
public static Namespacecontext Getnamespacecontext () throws Exception {
return new Namespacecontext () {
Public String Getnamespaceuri (String prefix) {
/*
//One way:
//Here you can use the configuration file to configure the xmlnamespace you want to use first,
// Registration is used for applications, which should be more efficient, without having to extract namespace from the document every time
//But no second convenience
String URI;
if (Prefix.equalsignorecase ("ML"))
URI = "mreml";
else if (prefix.equalsignorecase ("RE"))
URI = "http://herry.com.cn";
else if (prefix.equalsignorecase ("RID"))
URI = "ResourceID";
else if (prefix.equalsignorecase ("RUI"))
URI = "ResourceUniqueID";
else if (prefix.equalsignorecase ("Rmdi"))
URI = "Resourcemetadatainfo";
else
URI = null;
System.out.println (Parsexmlutil.class + ":: getnamespaceuri:prefix=" + prefix);
return URI;
*/
/*
//Another way: to extract the corresponding relationship between prefix and namespace by Prefixresolver
Final Prefixresolver resolver =
New Prefixresolverdefault (Doc.getdocumentelement ());
return Resolver.getnamespaceforprefix (prefix);
*/
Dummy Implementation-not used!
Public Java.util.Iterator getprefixes (String val) {
return null;
}
Dummy Implemenation-not used!
public string Getprefix (string uri) {
return null;
}
};
}
To find the value of a ResourceUniqueID, the XPath should be written as://ml:mreml/ml:resourceentity/rmdi:resourcemetadatainfo/rmdi:resourceid/ Rui:resourceuniqueid/text ()
Here the ml (prefix) and URI (MERML) have been passed
For documents that contain a default namespace, if Namespacecontext is used, the prefix and URI are mapped, such as for the following documents:
<mreml xmlns= "Mreml" ><resourceentity >
<ResourceMetaDataInfo>
<resourceid xmlns= "ResourceID" >
<rui:resourceuniqueid xmlns:rui= "ResourceUniqueID" > Program guid</rui:resourceuniqueid>
<userdefid>userdefid Fill out program code </UserDefID>
</ResourceID>
<Title>
<resourcename>911 News Material </ResourceName>
</Title>
<ResourceType>1</ResourceType>
<Subject>
<keywords>911 Attack </KeyWords>
</Subject>
<Description>
<ContentDescription> The United States encountered 911 attacks scene 30S screen and the same time sound material </ContentDescription>
<Column>
<ColumnName> World Today </ColumnName>
</Column>
</Description>
<Date>
<createdate>2006-10-25 13:12:10</createdate>
</Date>
<Format>
<TotalLength>00:04:35:12</TotalLength>
<NumberofElements>2</NumberofElements>
<MarkPoint>
<StartPoint>00:00:00:05</StartPoint>
<EndPoint>00:04:35:10</EndPoint>
</MarkPoint>
</Format>
</ResourceMetaDataInfo>
</ResourceEntity>
</MREML>
To find the value of a ResourceUniqueID, the XPath should be written as://ml:mreml/ml:resourceentity/rmdi:resourcemetadatainfo/rmdi:resourceid/ Rui:resourceuniqueid/text ()
Here the ml (prefix) and URI (MERML) have been passed
if (Prefix.equalsignorecase ("ML"))
URI = "Mreml";
else if (prefix.equalsignorecase ("RE"))
URI = "http://herry.com.cn";
else if (prefix.equalsignorecase ("RID"))
URI = "ResourceID";
else if (prefix.equalsignorecase ("RUI"))
Uri= "ResourceUniqueID";
else if (prefix.equalsignorecase ("Rmdi"))
URI = "Resourcemetadatainfo";
Else
URI = null;
Mapping.
Both ML and Rmdi are default namespace, and its scope contains its child elements until a new default namespace appears. and namespace only works on its own. If you:<resourcemetadatainfo> in the above document
<resourceid xmlns= "ResourceID" >
To
<rmdi:resourcemetadatainfo xmlns:rmdi= "Resourcemetadatainfo" >
<ResourceID>
...
...
</rmdi:resourcemetadatainfo>
To find the value of the ResourceUniqueID, the XPath should read://ml:mreml/ml:resourceentity/rmdi:resourcemetadatainfo/resourceid/ Rui:resourceuniqueid/text ()
We can also refer to this article:
http://blog.davber.com/2006/09/17/xpath-with-namespaces-in-java/