[xquery-talk] querying attribute?

G. Ken Holman gkholman at CraneSoftwrights.com
Wed Mar 18 20:05:33 PST 2009

At 2009-03-18 13:06 -0400, Thomas R. Jones wrote:
>Hello all, I have the following code:
><tests xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5">
>  <rpminfo_test
>check="all" comment="apache2 is installed"
>id="oval:com.maitreyasecurity:tst:674" version="1">
>   <object object_ref="oval:com.maitreyasecurity:obj:674"/>
>   <state state_ref="oval:com.maitreyasecurity:ste:500"/>
>  </rpminfo_test>
>I am trying everything i can think of to return the attribute value but
>am hanging up on the namespace(i think). I believe because the attribute
>is not prefixed that it is located within a NULL namespace and not the
>element default namespace declared----correct?

Yes, that is correct. *All* unprefixed attributes are in no 
namespace, never the default namespace.

>Here is my xquery:
>declare default element namespace
>"http://oval.mitre.org/XMLSchema/oval-definitions-5#linux"; for $attr in
>return string($attr)

You declare the "#linux" namespace as your default element namespace, 
so it is being applied to *every* element (after all, it is the 
default) which includes the <tests> element.  Your <tests> element is 
in not in the "#linux" namespace, so your problem is it isn't finding 
the element with the attribute, not that it isn't finding the attribute.

I tell my students that using the default namespace can be awkward 
when dealing with an instance of numerous namespaces (as I learned 
the hard way when working with UBL).  I believe it will make 
maintenance easier if you explicitly use prefixes when there is more 
than one namespace in your source tree.

A running example is below ... I hope this helps.

. . . . . . . . . Ken

t:\ftemp>type thomas.xml
<tests xmlns="http://oval.mitre.org/XMLSchema/oval-definitions-5">
check="all" comment="apache2 is installed"
id="oval:com.maitreyasecurity:tst:674" version="1">
   <object object_ref="oval:com.maitreyasecurity:obj:674"/>
   <state state_ref="oval:com.maitreyasecurity:ste:500"/>

t:\ftemp>call xquery thomas.xml thomas.xq
<?xml version="1.0" encoding="UTF-8"?>oval:com.maitreyasecurity:tst:674
t:\ftemp>type thomas.xq
declare namespace o =
declare namespace l =
  for $attr in
/o:tests/l:rpminfo_test/@id return string($attr)


XQuery/XSLT/XSL-FO training in Los Angeles (New dates!) 2009-06-08
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman at CraneSoftwrights.com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/q/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/q/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

More information about the talk mailing list