You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Sanjoy Ghosh <sa...@yahoo.com> on 2004/10/24 19:17:34 UTC

XPath with default namespace

Hi,

I am essentially using the code from SimpleXPathAPI sample in the Xalan-C_1_8_0-windows_2000-msvc_60 samples programs to evaluate a series of XPath expressions.  The XML document that I am evaluating the XPath expressions against uses a bunch of XML namespaces.  I find that even for elements in the default namespace I have to qualify the element name with the namespace prefix in the XPath expression for it to work.  Is there any way to make XPathEvaluator() recognize element names without a prefix as being in the default namespace?

The code that I am using is:

  XalanSourceTreeDOMSupport  theDOMSupport;
  XalanSourceTreeParserLiaison theLiaison( theDOMSupport);
  theDOMSupport.setParserLiaison( &theLiaison);

  const XalanDOMString   theFileName( _fileName.c_str(), _fileName.length());
  const LocalFileInputSource  theInputSource( theFileName.c_str());
  XalanDocument* const   theDocument = theLiaison.parseXMLStream( theInputSource);
  assert( theDocument != 0);
  XalanDocumentPrefixResolver  thePrefixResolver( theDocument);
  XPathEvaluator     theEvaluator;

   XalanNode* xpathNode = getXPathEvaluator().selectSingleNode( getDOMSupport(), node, 
     XalanDOMString( xpathIterator->getXPath().c_str()).c_str(), getPrefixResolver());


The XML document I am using is:

<FpML

version="4-1"

dswgVersion = "0-4"

xsi:type="dswg:PositionReport"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:fpml = "http://www.fpml.org/2004/FpML-4-1"

xmlns:dswg = "http://www.dswg.org/position-reporting"

xmlns:dsig = "http://www.w3.org/2000/09/xmldsig#"

xsi:schemaLocation="http://www.fpml.org/2004/FpML-4-1 schema/fpml-main-4-1.xsd http://www.dswg.org/position-reporting schema/dswg-0-4.xsd"

xmlns="http://www.fpml.org/2004/FpML-4-1">


The XPath I am trying to evaluate is:

dswg:positionSummary/fpml:equitySwap/fpml:productType

"fpml" is the default namespace.  But if I remove the prefix from the expression above it doesn't work.

Is there any way to make the following evaluate properly?

dswg:positionSummary/equitySwap/productType



Thanks,

Sanjoy

RE: XPath with default namespace

Posted by Rick Bullotta <ri...@lighthammer.com>.
>> I don't think it's a huge burden for the author of an XPath expression to

>> specify what sort of elements he/she is looking for.

The challenge, however, is that these "expressions" cannot, at present, be
uniformly represented or persisted, since the syntactic representation of
the Xpath (the actual path, including prefix) is "usage dependent" in the
tool/application used at the time.  If application #1 chooses to use prefix
NS1 for a certain namespace URI, and the path is /NS1:Thing/doc:Foo, this
path is not likely portable to another application which may handle the
default namespaces completely differently.  Therein lies the challenge, I
think!?

Rick


-----Original Message-----
From: david_n_bertoni@us.ibm.com [mailto:david_n_bertoni@us.ibm.com] 
Sent: Monday, October 25, 2004 2:32 PM
To: xalan-dev@xml.apache.org
Subject: RE: XPath with default namespace

> Aren't default namespaces just EVIL? ;)

Yes, I wish they weren't used so heavily, but XHTML started it, and now 
we're stuck.

> I always felt that this was a near-fatal flaw in the binding
> between XML and Xpath/XSLT...as it creates the need for a
> "programmer" to consciously "add" something to a DOM, parser
> or other component to enable Xpathing anything with a default
> namespace somewhere in the path.  And since the assigned
> prefix is determined by the programmer, it is essentially
> "non-portable" - the context used for an Xpath expression in
> product "A" might not be the same as product "B".

I think it's the only rational thing to do given default namespaces. Since 
the prefix is irrelevant, as long as it's bound to the correct namespace 
URI, there's no danger of getting unexpected results.  Imagine what would 
happen if the default namespace _did_ apply, and you got a document with a 
different default namespace then you were expecting -- what a mess.

I don't think it's a huge burden for the author of an XPath expression to 
specify what sort of elements he/she is looking for.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


RE: XPath with default namespace

Posted by da...@us.ibm.com.
> Aren't default namespaces just EVIL? ;)

Yes, I wish they weren't used so heavily, but XHTML started it, and now 
we're stuck.

> I always felt that this was a near-fatal flaw in the binding
> between XML and Xpath/XSLT...as it creates the need for a
> "programmer" to consciously "add" something to a DOM, parser
> or other component to enable Xpathing anything with a default
> namespace somewhere in the path.  And since the assigned
> prefix is determined by the programmer, it is essentially
> "non-portable" - the context used for an Xpath expression in
> product "A" might not be the same as product "B".

I think it's the only rational thing to do given default namespaces. Since 
the prefix is irrelevant, as long as it's bound to the correct namespace 
URI, there's no danger of getting unexpected results.  Imagine what would 
happen if the default namespace _did_ apply, and you got a document with a 
different default namespace then you were expecting -- what a mess.

I don't think it's a huge burden for the author of an XPath expression to 
specify what sort of elements he/she is looking for.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


RE: XPath with default namespace

Posted by Rick Bullotta <ri...@lighthammer.com>.
Aren't default namespaces just EVIL? ;)

I always felt that this was a near-fatal flaw in the binding between XML and
Xpath/XSLT...as it creates the need for a "programmer" to consciously "add"
something to a DOM, parser or other component to enable Xpathing anything
with a default namespace somewhere in the path.  And since the assigned
prefix is determined by the programmer, it is essentially "non-portable" -
the context used for an Xpath expression in product "A" might not be the
same as product "B".

But as well all know, there isn't an easy solution...other than not using
default namespaces or providing a prefix mapping in the source document for
each default namespace URI.

Any other recommended "best practices"?

Rick Bullotta
CTO
Lighthammer Software (http://www.lighthammer.com)


-----Original Message-----
From: david_n_bertoni@us.ibm.com [mailto:david_n_bertoni@us.ibm.com] 
Sent: Sunday, October 24, 2004 2:36 PM
To: xalan-dev@xml.apache.org
Subject: Re: XPath with default namespace

> I am essentially using the code from SimpleXPathAPI sample in the
> Xalan-C_1_8_0-windows_2000-msvc_60 samples programs to evaluate
> a series of XPath expressions.  The XML document that I am
> evaluating the XPath expressions against uses a bunch of XML
> namespaces.  I find that even for elements in the default
> namespace I have to qualify the element name with the namespace
> prefix in the XPath expression for it to work.  Is there any way
> to make XPathEvaluator() recognize element names without a prefix
> as being in the default namespace?

No, there isn't.  You must use a prefix for the default namespace.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: XPath with default namespace

Posted by Joseph Kesselman <ke...@us.ibm.com>.



>  /*[namespace-uri()='http://www.example.com/foo' and
> local-name()='bar']/*[namespace-uri()='http://www.example.com/foo' and
> local-name()='moo']

Uhmph. Yeah, that does work, and a Sufficiently Clever Processor (TM) ought
to do the right thing with it. I don't think Xalan will process it
optimally but maybe we should consider making it do so.

Not particularly convenient for humans to either write or read, though. I'd
prefer a syntax that communicates well with both people and machines.


______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: XPath with default namespace

Posted by Richard Zschech <ri...@cqrdata.com>.
Joseph Kesselman wrote:

>
>
>  
>
>>>namespace I have to qualify the element name with the namespace
>>>prefix in the XPath expression for it to work.  Is there any way
>>>to make XPathEvaluator() recognize element names without a prefix
>>>as being in the default namespace?
>>>      
>>>
>>No, there isn't.  You must use a prefix for the default namespace.
>>    
>>
>
>I'm told XPath 2.0 is proposing to add a default-namespace mechanism.
>
>I don't know whether they're going to do anything about my own pet peeve,
>which is that there is no syntax in XPath itself for binding namespaces and
>hence it's impossible to write an XPath that is completely self-contained
>and portable; some (unspecified) source for the prefix-to-namespace mapping
>is always required.
>  
>
You actually can write an xpath which is completely self-contained and 
portable but its messy eg:

/ex:foo/ex:bar

can be written as:

/*[namespace-uri()='http://www.example.com/foo' and 
local-name()='bar']/*[namespace-uri()='http://www.example.com/foo' and 
local-name()='moo']

and will work anywhere :-)

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: XPath with default namespace

Posted by Joseph Kesselman <ke...@us.ibm.com>.



>> namespace I have to qualify the element name with the namespace
>> prefix in the XPath expression for it to work.  Is there any way
>> to make XPathEvaluator() recognize element names without a prefix
>> as being in the default namespace?

>No, there isn't.  You must use a prefix for the default namespace.

I'm told XPath 2.0 is proposing to add a default-namespace mechanism.

I don't know whether they're going to do anything about my own pet peeve,
which is that there is no syntax in XPath itself for binding namespaces and
hence it's impossible to write an XPath that is completely self-contained
and portable; some (unspecified) source for the prefix-to-namespace mapping
is always required.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: XPath with default namespace

Posted by da...@us.ibm.com.
> I am essentially using the code from SimpleXPathAPI sample in the
> Xalan-C_1_8_0-windows_2000-msvc_60 samples programs to evaluate
> a series of XPath expressions.  The XML document that I am
> evaluating the XPath expressions against uses a bunch of XML
> namespaces.  I find that even for elements in the default
> namespace I have to qualify the element name with the namespace
> prefix in the XPath expression for it to work.  Is there any way
> to make XPathEvaluator() recognize element names without a prefix
> as being in the default namespace?

No, there isn't.  You must use a prefix for the default namespace.

Dave

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


XPathEvaluator throws unknown exception

Posted by Sanjoy Ghosh <sa...@yahoo.com>.
Hello,

I am using the following code to evaluate a vector of XPath strings against a context node:

 XalanSourceTreeDOMSupport& getDOMSupport () {return *_domSupport;}
 XalanDocumentPrefixResolver& getPrefixResolver () {return *_prefixResolver;}
 XPathEvaluator& getXPathEvaluator () {return *_xpathEvaluator;}


void FpMLWalker::processPositionNode (XalanNode* node) {
 int count = 0;
 vector<XPathItem>::iterator xpathIterator;
 for (xpathIterator = _xpathItems.begin(); xpathIterator != _xpathItems.end(); xpathIterator++) {
  try {
   cout << count++ << "  " << xpathIterator->getXPath() << "  ";
   XalanNode* xpathNode = getXPathEvaluator().selectSingleNode( getDOMSupport(), node, 
     XalanDOMString( xpathIterator->getXPath().c_str()).c_str(), getPrefixResolver());
   if (xpathNode != NULL) {
    if (xpathNode->getFirstChild() != NULL)
     cout << xpathNode->getFirstChild()->getNodeValue() << endl;
    else
     cout << xpathNode->getNodeValue() << endl;
   }
   else
    cout << "No Result" << endl;
  }
  catch (...) {cout << "WHAT" << endl;}
 }
 cout << endl;

}


This function is called with a series of context nodes which have the same structure and the same namespace and element tag.  The XPath expressions are also the same.  But after the first few calls, it throws some exception which is caught by catch (...).  After that it always throws an exception and is never able to recover.

How do I find out who is throwing the exception, and what exception is being thrown?  I couldn't find any "throws" clause in the API.  The XPathCApi also catches a generic exception.  Is there any way to find out who is throwing the exception, and why?  And is there a fix for this :)

Thanks,

Sanjoy