2011-12-06 58 views
1

下面是XML文件 -在XSLT中獲取錯誤的輸出?

<Seminars> 
    <Seminar> 
    <Venue P="ABC" dt="20111223"/> 
    <Subject name="Finance"> 
     <Topic main="Bonds"/> 
     <Topic main="Stocks" sub="Technical Analysis"/> 
    </Subject>  
    </Seminar>  
    <Seminar> 
    <Venue P="ABC" dt="20111225"/> 
    <Subject name="Yoga"> 
     <Topic main="Benefits"/> 
    </Subject> 
    <Subject name="Meditation"> 
     <Topic main="Benefits"/> 
    </Subject> 
    </Seminar> 
    <Seminar> 
    <Venue P="PQR" dt="20111220"/>  
    <Subject name="IT"> 
     <Topic main="Java" sub="Swing"/> 
     <Topic main="Java" sub="NIO"/> 
    </Subject>  
    </Seminar> 
    <Seminar> 
    <Venue P="ABC" dt="20111224"/> 
    <Subject name="Medical"> 
     <Topic main="Plastic Surgery"/> 
     <Topic main="Mal-nutrition"/> 
    </Subject> 
    <Subject name="IT"> 
     <Topic main="Java" sub="Collections"/> 
     <Topic main="Web Technologies"/> 
    </Subject>  
    </Seminar> 
    <Seminar>  
    <Venue P="XYZ" dt="20111226"/> 
    <Subject name="IT"> 
     <Topic main="DotNET - I"/> 
     <Topic main="DotNET - II"/> 
     <Topic main="XML" sub="Security"/> 
    </Subject>  
    </Seminar> 
    <Seminar> 
    <Venue P="XYZ" dt="20111227"/> 
    <Subject name="IT"> 
     <Topic main="Oracle"/> 
     <Topic main="Oracle" sub="JDeveloper"/> 
    </Subject>  
    </Seminar> 
</Seminars> 

下面是Java程序 -

import org.w3c.dom.*; 
import javax.xml.xpath.*; 
import javax.xml.parsers.*; 
import java.io.IOException; 
import org.xml.sax.SAXException; 

public class Seminar 
{ 
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException 
    { 
     DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
     domFactory.setNamespaceAware(true); 
     DocumentBuilder builder = domFactory.newDocumentBuilder(); 
     Document doc = builder.parse("seminar.xml"); 
     XPath xpath = XPathFactory.newInstance().newXPath(); 

     String qry = "//Seminars/Seminar[contains(Subject/@name,'Medical')]/Subject/Topic/@main"; 
     XPathExpression expr = xpath.compile(qry); 
     Object result = expr.evaluate(doc, XPathConstants.NODESET); 
     NodeList nodes = (NodeList) result; 

     for (int i = 0; i < nodes.getLength(); i++) 
     { 
      System.out.println(nodes.item(i).getNodeValue()); 
     } 
    } 
} 

使用上面的程序我期待輸出 -

Plastic Surgery 
Mal-nutrition 

但我也越來越下一個Subject標籤的輸出屬性值爲IT ... ???

我在查詢中缺少什麼...?

回答

3

您的查詢應該是

String qry = "//Seminars/Seminar/Subject[contains(@name,'Medical')]/Topic/@main"; 

你問所有的Seminar@name包含與「醫學」一Subject的主題。新查詢請求在@name中包含「Medical」的Subject中的所有Topics。微妙但重要的區別!

+0

+1,因爲這比@詹姆斯古德溫的 –

2

您的XPath會爲您提供所有主題/主題[@main],任何研討會至少有一個名爲醫療的主題。

你可能想

String qry = "//Seminars/Seminar/Subject[contains(@name,'Medical')]/Topic/@main"; 

String qry = "//Seminars/Seminar/Subject[@name='Medical']/Topic/@main"; 
+0

不太脆弱的解決方案的第二個'qry'是帶有開口失蹤「(」請編輯並更正 – John

+0

@John它實際上有一個關閉「)」太多。感謝您的發現。 –