Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2008, 12:08 AM
Authorized User
 
Join Date: Jun 2005
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default Xpath functions

Trying to select all cd elements whose releasedate is on a specific month using
doc.Selectnodes("/cd/releasedate[fn:month-from-date(.)=04]")
but I always get this exception

Unhandled Exception: System.Xml.XPath.XPathException: XsltContext is needed for
this query because of an unknown function.

How do I fix this? I am using this sample xml:

<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
    <releasedate>2008-04-06T11:03:02</releasedate>
  </cd>
  <cd format="vcd">
    <title>VCD Empire Burlesque</title>
    <artist>VCD Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
    <releasedate>2008-05-08T11:03:02</releasedate>
  </cd>
</catalog>
__________________
\"Dont you ever give up!\"
 
Old August 25th, 2008, 02:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

fn:month-from-date is a function, in a namespace defined by the prefix "fn". But you don't define the "fn" prefix. You probably need to pass in a XmlNamespaceManager instance as a second parameter to the SelectNodes method.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 25th, 2008, 03:06 AM
Authorized User
 
Join Date: Jun 2005
Posts: 97
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to aldwinenriquez
Default

XmlDocument doc = new XmlDocument();
            doc.Load(@"..\..\x1.xml");
            XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
            xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            man.AddNamespace("fn", "http://www.w3.org/2005/02/xpath-functions");
            man.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            man.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XmlNodeList nodes = doc.DocumentElement.SelectNodes(xpath,man);

Yes, I have tried this too, but without any lock..


"Dont you ever give up!"
 
Old August 25th, 2008, 03:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You are linking to the XPATH 2.0 functions - .Net Framework only support XPATH 1.0.

/- Sam Judson : Wrox Technical Editor -/
 
Old August 25th, 2008, 03:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The name of the exception System.Xml.XPath.XPathException suggests that you are trying to do this using the XPath engine in .NET (it seems to be an endemic problem on this forum that people expect us to guess what products they are using...). This engine only supports XPath 1.0, which has no month-from-date() function.

Try Saxon instead, which runs under .NET and supports XPath 2.0.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Some XPath functions not available ole_v2 XSLT 4 March 29th, 2007 05:25 AM
Xpath help please rishid XML 1 February 28th, 2007 03:23 AM
Using Xpath functions Tomi XSLT 1 July 21st, 2006 05:43 AM
XPath: String functions question. pupu79 XSLT 4 January 10th, 2005 03:00 PM
help with XPath kend XSLT 2 July 15th, 2003 01:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.