2011-11-14 129 views
1

有沒有一種方法可以從XML文件中檢索所有使用XPath的非空節點?該XML看起來是這樣的:java xpath解析

<workspace> 
<light> 
    <activeFlag>true</activeFlag> 
    <ambientLight>0.0:0.0:0.0:0.0</ambientLight> 
    <diffuseLight>1.0:1.0;1.0:1.0</diffuseLight> 
    <specularLight>2.0:2.0:2.0:2.0</specularLight> 
    <position>0.1:0.1:0.1:0.1</position> 
    <spotDirection>0.2:0.2:0.2:0.2</spotDirection> 
    <spotExponent>1.0</spotExponent> 
    <spotCutoff>2.0</spotCutoff> 
    <constantAttenuation>3.0</constantAttenuation> 
    <linearAtenuation>4.0</linearAtenuation> 
    <quadricAttenuation>5.0</quadricAttenuation> 
</light> 

<camera> 
    <activeFlag>true</activeFlag> 
    <position>2:2:2</position> 
    <normal>1:1:1</normal> 
    <direction>0:0:0</direction> 
</camera> 

<object> 
    <material>lemn</material> 
    <Lu>1</Lu> 
    <Lv>2</Lv> 
    <unit>metric</unit> 
    <tip>tip</tip> 
    <origin>1:1:1</origin> 
    <normal>2:2:2</normal> 
    <parent> 
     <object>null</object> 
    </parent> 
    <leafs> 
     <object>null</object> 
    </leafs> 
</object> 

每個標籤之後解析器「看見」,我並不需要另一個空節點。

+0

凡在提供XML空節點? –

+0

它不「看」它。它真的在那裏。這是一個文本節點,它包含(對於大多數行,在文檔的情況下)後面跟着4個空格。 – dty

+0

每兩個標籤之間。對於每一個新行都有一個空節點。 – MRM

回答

2

我猜你想要的是有立竿見影的文本子節點,不包括純粹的白色空間中的所有元素節點:

//*[string-length(normalize-space(text())) > 0] 
+0

的確,非常感謝! – MRM

0

如果您使用XSLT,使用<xsl:strip-space elements="*"/>。如果你不是,這取決於你使用的是什麼技術(你沒有告訴我們),例如。 DOM,JDOM等

0

你想

//*[normalize-space()] 

表達

//*[string-length(normalize-space(text())) > 0] 

是一個錯誤的答案。它選擇文檔中第一個文本節點子文本不是僅包含空白的文本中的所有元素。

因此,這不會選擇

<p><b>Hello </b><i>World!</i></p> 

雖然本款包含了相當多的文字...