XSLT1中使用XPATH对含有默认名称空间声明的XML文档进行查找时是查不出内容的
例如对于
<requestHierarchySelectResult xmlns
=http://www.lightcc.com/ns xmlns
:cs
="http://www.customsolids.com">
<request
>
<created_dt
>05/05/2000 00:00:00</created_dt
>
<created_tm
>01/01/1900 14:02:46</created_tm
>
<cs
:request_id
>100002</cs
:request_id
>
</request
>
</requestHierarchySelectResult
>
应用
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns="http://www.lightcc.com/ns"
xmlns:cs="http://www.customsolids.com">
<xsl:template match="/">
<root><xsl:apply-templates select="/*/request"/></root>
</xsl:template>
<xsl:template match="*|@*|node()"/>
<xsl:template match="request">
<gotHere><xsl:value-of select="."/></gotHere>
</xsl:template>
</xsl:stylesheet>
是没有效果的.
需要给默认的名称空间指定一个前缀
<
xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:lc="http://www.lightcc.com/ns"
xmlns:cs="http://www.customsolids.com">
<xsl:template match="/">
<root><xsl:apply-templates select="/*/lc:request"/></root>
</xsl:template>
<xsl:template match="*|@*|node()"/>
<xsl:template match="lc:request">
<gotHere><xsl:value-of select="."/></gotHere>
</xsl:template>
</xsl:stylesheet>
据说XSLT2和XPATH2会解决这个问题
参考:
http://www.edankert.com/defaultnamespaces.html http://www.topxml.com/people/bosley/defaultns.asp
项目中用到XMLBeans做XML的数据绑定工作,但是发现生成的代码中的selectPath并不工作,调查后发现要使用SAXON,但是下载最新版后发现仍然不能正常工作.又经过调查得以解决.
XMLBeans2.2版是和saxon的8.6.1版一起工作的。最初的8.7和GOOGLE到的解决方案中为2.0版准备的8.1都不行
XMLBeans2.2:
http://mirror.vmmatrix.net/apache/xmlbeans/binaries/
saxon 8.6.1:
http://jaist.dl.sourceforge.net/sourceforge/saxon/saxonb8-6-1.zip
xpath查询至少需要 saxon8.jar saxon8-dom.jar saxon8-xpath.jar才能正常工作
缺少saxon8.jar会报如下错误
java.lang.RuntimeException: Trying XBeans path engine... Trying XQRL... Trying Saxon... FAILED on $this/data[@name='data2']
at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:131)
at org.apache.xmlbeans.impl.store.Path.getCompiledPath(Path.java:91)
at org.apache.xmlbeans.impl.store.Cursor._selectPath(Cursor.java:902)
at org.apache.xmlbeans.impl.store.Cursor.selectPath(Cursor.java:2634)
at org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:431)
at org.apache.xmlbeans.impl.values.XmlObjectBase.selectPath(XmlObjectBase.java:415)
at test.main(test.java:16)
Exception in thread "main"
只放saxon8.jar也是不正确的.缺少其他2个jar查询结果会不正确
简单测试:
SCHEMA:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root" type="rootType">
<xs:annotation>
<xs:documentation>rootcomment</xs:documentation>
</xs:annotation>
</xs:element>
<xs:complexType name="dataType">
<xs:attribute name="name" use="required"/>
</xs:complexType>
<xs:complexType name="datasType">
<xs:sequence>
<xs:element name="data" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:complexType name="rootType">
<xs:sequence>
<xs:element name="datas" type="datasType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
XML:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Z:\test.xsd">
<datas>
<data name="data1">value1</data>
<data name="data2">value2</data>
<data name="data3">value3</data>
</datas>
</root>
jest.java
import java.io.File;
import noNamespace.DatasType;
import noNamespace.RootDocument;
import org.apache.xmlbeans.XmlObject;
public class test {
final static String NS_DECLEAR = "";
public static void main(String[] args) throws Exception {
RootDocument doc = RootDocument.Factory.parse(new File("z:\\test.xml"));
DatasType datas = doc.getRoot().getDatas();
XmlObject[] objs = datas.selectPath("$this/data[@name='data2']");
for (int i = 0; i < objs.length; i++) {
System.out.println(objs[i]);
}
}
}
输出:
<xml-fragment name="data2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">value2</xml-fragment>