2012-03-27 45 views
1

我要選擇符合特定條件的節點和處理這些節點之後的列表中排除節點,我想選擇其餘的。我如何在XSLT和XPath中執行此操作。XSLT/XPath的:從什麼是選擇

下面是場景,我有這樣的XML

<books> 
<book name="Basic XML"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic XML"> 
    <type>Tutorial</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic XSLT"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic XSLT"> 
    <type>Tutorial</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic Java"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic Java"> 
    <type>Tutorial</type> 
    <grouping>A</grouping> 
</book> 
<book name="Web Service"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
<book name="C Programming"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
</books> 

1. 「教程」 的<type>選擇所有<book>節點,下面是輸出

<books> 
<book name="Basic XML"> 
    <type>Tutorial</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic XSLT"> 
    <type>Tutorial</type> 
    <grouping>A</grouping> 
</book> 
<book name="Basic Java"> 
    <type>Tutorial</type> 
    <grouping>A</grouping> 
</book> 
</books> 

2.然後選擇其他<book>節點沒有「教程」的<type>,並且與#1中選擇的節點不相同@name,輸出僅爲:

<books> 
<book name="Web Service"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
<book name="C Programming"> 
    <type>Educational</type> 
    <grouping>A</grouping> 
</book> 
</books> 

回答

1

對於第一個查詢:

<xsl:apply-templates select="/books/book[type='Tutorial']"/> 

對於第二個查詢:

<xsl:apply-templates select="/books/book[type!='Tutorial']"/> 

那麼你就需要相應的模板來處理它們:

<xsl:template match="/books/book[type='Tutorial']"> 
    Do Something... 
</xsl:template> 

,最終片將是檢查當前節點是否還具有一個教程節點

<xsl:template match="/books/book[type!='Tutorial']"> 
    <xsl:variable name="bookname"> 
     <xsl:value-of select="@name"/> 
    </xsl:variable> 
    <xsl:if test="count('/books/book[@name=$bookname and type='Tutorial']')=0"> 
     Do Something... 
    </xsl:if> 
</xsl:template> 
+0

謝謝您的回答 – Ianthe 2012-04-18 07:42:08

0

這裏是一個完整的解決方案

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/*"> 
    <xsl:variable name="vTutBooks" select="book[type = 'Tutorial']"/> 
    <books> 
    <xsl:apply-templates select="$vTutBooks"/> 
    </books> 

    <books> 
    <xsl:apply-templates select= 
    "book[not(type = 'Tutorial') 
     and 
     not(@name = $vTutBooks/@name) 
     ]"/> 
    </books> 
</xsl:template> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔施加:

<books> 
    <book name="Basic XML"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic XML"> 
     <type>Tutorial</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic XSLT"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic XSLT"> 
     <type>Tutorial</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic Java"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic Java"> 
     <type>Tutorial</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Web Service"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="C Programming"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
</books> 

有用,正確的結果產生

<books> 
    <book name="Basic XML"> 
     <type>Tutorial</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic XSLT"> 
     <type>Tutorial</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="Basic Java"> 
     <type>Tutorial</type> 
     <grouping>A</grouping> 
    </book> 
</books> 
<books> 
    <book name="Web Service"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
    <book name="C Programming"> 
     <type>Educational</type> 
     <grouping>A</grouping> 
    </book> 
</books>