John
John

Reputation: 2852

Getting wrong output in XSLT?

Below is the XML file -

<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>

Below is the Java program -

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());
      }
   }
}

Using the above program I was expecting output as -

Plastic Surgery
Mal-nutrition

But I am also getting the output for the next Subject Tag which is having attribute value as IT ...???

What am I missing in the query...?

Upvotes: 1

Views: 91

Answers (2)

Roger Lindsj&#246;
Roger Lindsj&#246;

Reputation: 11553

Your XPath will give you all Subject/Topic[@main] for any Seminar which has at least one Subject with name Medical.

You probably want

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

or

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

Upvotes: 2

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

Your query should be

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

You were asking for all the topics in the Seminar containing a Subject with "Medical" in the @name. The new query asks for all the Topics in the Subject that contains "Medical" in the @name. Subtle, but important difference!

Upvotes: 3

Related Questions