How can we write case insensitive queries for Xpath xml parsing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: How can we write case insensitive queries for Xpath xml parsing

  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Post How can we write case insensitive queries for Xpath xml parsing

    Please follow the given xml code below.

    Consider a xml file with parameter and values like


    <Book>
    <Name>ABC</NAME>
    <Value>123</Value>
    </Book>
    <book>
    <Name>EFG</Name>
    <Value>456</Value>
    </book>
    <bOOK>
    <Name>XYZ</Name>
    <Value>789</Name>
    </bOOK>

    In Java,
    How can we write a single Xpath query to parse all the book blocks in single expression though they are in different case(Upper case/Lowercase).
    Please let me know if anybody can help me with this.

    Regards,
    Vijay

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: How can we write case insensitive queries for Xpath xml parsing

    XPath is used to query well formed XML documents. Your XML isn't well formed, so I'm not sure you can use XPath here.

    - petter
    I love deadlines. I like the whooshing sound they make as they pass by - Douglas Adams.
    Visit me!.

    Use code-tags! [code]Your code here[/code]

  3. #3
    Join Date
    Sep 2008
    Posts
    4

    Post Re: How can we write case insensitive queries for Xpath xml parsing

    Please note: Updating the xml code correctly

    <config>
    <Book>
    <Name>ABC</Name>
    <Value>123</Value>
    </Book>
    <book>
    <Name>EFG</Name>
    <Value>456</Value>
    </book>
    <bOOK>
    <Name>XYZ</Name>
    <Value>789</Value>
    </bOOK>
    </config>


    Regards,
    Vijay

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How can we write case insensitive queries for Xpath xml parsing

    Assuming the XML is well-formed, this expression will find all nodes whose lowercase name is 'book':

    //.[lower-case(name())='book']

    The key to performance is elegance, not batallions of special cases...
    J. Bently & D. McIlroy
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Sep 2008
    Posts
    4

    Post Re: How can we write case insensitive queries for Xpath xml parsing

    Please see the result of the modification, I am getting some errors, What might be the cause

    XPathExpression bookexp = xpath.compile("//[lower-case(name())='book']/Name/text()");
    Object obj = bookexp.evaluate(doc, XPathConstants.NODESET);


    Output:

    Exception in thread "main" javax.xml.transform.TransformerException: A location step was expected following the '/' or '//' token.
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source)
    at Gts.query(Gts.java:38)
    at Gts.main(Gts.java:46)

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How can we write case insensitive queries for Xpath xml parsing

    You didn't copy the expression I posted. Look again (or just copy & paste).

    Everything should be made as simple as possible, but not simpler...
    A. Einstein
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Sep 2008
    Posts
    4

    Post Re: How can we write case insensitive queries for Xpath xml parsing

    Hi...

    Please see the result of the expression posted by you.

    bookexp = xpath.compile("//.[lower-case(name())='book']");
    Object obj = bookexp.evaluate(doc, XPathConstants.NODESET);


    Please view the full code.

    import java.io.IOException;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;

    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;


    public class Gts {

    public void query() throws ParserConfigurationException, SAXException,
    IOException, XPathExpressionException {
    // Standard of reading a XML file
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    DocumentBuilder builder;
    Document doc = null;

    //Expression variables to check xml parameters
    XPathExpression bookexp = null; // Expression to parse global parameters names

    XPathFactory xFactory = XPathFactory.newInstance();

    // Create a XPath object
    XPath xpath = xFactory.newXPath();

    // Compile the XPath expression
    builder = factory.newDocumentBuilder();
    doc = builder.parse("c:/book.xml");
    bookexp = xpath.compile("//.[lower-case(name())='book']");
    Object obj = bookexp.evaluate(doc, XPathConstants.NODESET);
    NodeList n = (NodeList) obj;
    for (int i=0; i<n.getLength(); i++)
    System.out.println(n.item(i).getNodeValue());
    }
    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
    Gts g = new Gts();
    g.query();
    }

    }


    Please let me know if any modifications.

    Output:

    Exception in thread "main" javax.xml.transform.TransformerException: '..[predicate]' or '.[predicate]' is illegal syntax. Use 'self::node()[predicate]' instead.
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(Unknown Source)
    at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
    at com.sun.org.apache.xpath.internal.XPath.<init>(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.compile(Unknown Source)
    at Gts.query(Gts.java:38)
    at Gts.main(Gts.java:46)

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: How can we write case insensitive queries for Xpath xml parsing

    The expression worked fine for me on the sample XML you provided. If it doesn't work for you, why not try the suggestion in the error message?

    The purpose of computing is insight, not numbers...
    R. Hamming
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center