2015-07-10 49 views
1

我需要確定元素包含隨機文本字符串後跟特定子元素(也包含隨機字符串)的情況。例如:確定XML節點是否包含文本,後跟使用xpath/xslt的特定子元素

<paragraph>Here's some text and here's a <word>child</word></paragraph> 

這是樣式表的一部分,所以應該使用xpath。

+0

你的問題不清楚。 –

+0

@ michael.hor257k對我來說這很清楚...... – o11c

+0

@ o11c:那麼你是透徹的,因爲這個問題是模棱兩可的。 OP:首先澄清你的意思是「隨機」(兩次),以及「跟隨」是否是必要的,以及它是否立即隱含。 – kjhughes

回答

0

您正在尋找軸following-sibling::軸。

有了這個XML文檔:

<root> 
    <paragraph>Here's some text and here's a <word>child</word>.</paragraph> 
    <paragraph>Here's some text with no child.</paragraph> 
    <paragraph>Here's some text with another <word>child</word>.</paragraph> 
    <paragraph/> 
    <paragraph>Here's some text with any empty <word/>.</paragraph> 
</root> 

下面的XPath表達式選擇的第一,第三和第五段落:

//paragraph[text()[following-sibling::word]] 

如果要約束word標籤也有一個直接文字的孩子,用這個只選第一個和第三個:

//paragraph[text()[following-sibling::word[text()]]] 
0

請嘗試:

//*[text()/following-sibling::*/text()] 
相關問題