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="?<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="?<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=">
?