2016-06-14 109 views
0

在下面的輸入XML中,我想匹配entryRelationship元素與代碼10例如(或id,因爲這些元素都是唯一的)。與某些屬性匹配的兄弟

<?xml version="1.0" encoding="UTF-8"?> 
<Document> 
    <component> 
     <section> 
      <id test= "1"/> 
      <code code="10"/> 
      <entry> 
       <entryRelationship> 
        <time value="first time"/>         
       </entryRelationship> 
      </entry> 
     </section> 

     <section> 
      <id test= "2"/> 
      <code code="11"/> 
      <entry> 
       <entryRelationship> 
        <time value="second time"/>        
       </entryRelationship>                 
      </entry> 
     </section> 
    </component>       
</Document> 

我可以寫一個模板,如xsl:template match = "Document/component/section/code[@code = '10']/entry/entryRelationship。雖然問題是入口不是代碼的孩子,而是兄弟姐妹。我該如何解決這個問題?

回答

4

雖然問題是該條目不是代碼的孩子,而是兄弟姐妹。我該如何解決這個問題?

謂詞可以嵌套。

<xsl:template match="section[code[@code=10]]/entry/entryRelationship"> 

另外,不嵌套:

<xsl:template match="section[code/@code=10]/entry/entryRelationship"> 

或者這樣

<xsl:template match="code[@code=10]/../entry/entryRelationship"> 

或者這樣

<xsl:template match="entryRelationship[../../code/@code=10]"> 

有這麼多的方式來表達這一點。

+0

謝謝,但不幸的是我得到的錯誤「的屬性節點(值)不能包含元素的子之後創建。 「 – Enigma

+0

這與我的答案中的代碼完全沒有關係,但卻是一個完全獨立的問題。 – Tomalak

+0

是的,我也這麼認爲,儘管它在某種意義上有點相關,我只是在添加這個模板後纔得到它,並且每當我評論它時,錯誤都會消失。但是,謝謝。 – Enigma

1

雖然問題是該條目不是代碼的孩子,而是兄弟姐妹。我該如何解決這個問題?

的輸入與代碼段的孩子:爲此嘗試:

<xsl:template 
     match="Document/component/section[code/@code = '10']/entry/entryRelationship" />