[erlang-questions] xmerl_xpath and namespaces

bryan rasmussen <>
Tue Oct 7 13:53:13 CEST 2008


Hi Denis,



2008/10/3 denis <>:
> Hello,
>
>
>
> I'm having some problem using xmerl_xpath on a document having namespaces.
> I'm not sure I fully understand how all of this work and the problem should
> not come from xmerl_xpath, but from my misunderstanding of xml, namespace,
> xpath…
>
> It seems that with xpath, it's mandatory to give the namespace.
>
>
> For instance, in a document like this:
>
>
>
> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified"
>
>
>
> targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">
>
>   <xs:simpleType name="char">
>
>       <xs:restriction base="xs:int"/>
>
>   </xs:simpleType>
>
>   <xs:element name="duration" nillable="true" type="tns:duration"/>
>
>   <xs:simpleType name="duration">
>
>       <xs:restriction base="xs:duration">
>
>           <xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?"/>
>
>               <xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>
>
>               <xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>
>
>       </xs:restriction>
>
>   </xs:simpleType>
>
>   <xs:element name="guid" nillable="true" type="tns:guid"/>
>
> </xs:schema>
>
>
>
> If I want to get all "element" attributes, I need to do a query like
>
> xmerl_xpath:string("//xs:simpleType", X)
>
> Is there a way to not having to give the namespace in the query ??

This XPath should get it //*[local-name()='simpleType']

but that would also get any element where the local name (xpath term
for dom basename) of an element was simpleType so if your instance was
this

<xs:schema attributeFormDefault="qualified"
elementFormDefault="qualified"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:blah="http://example.org/blah"

targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/">

  <xs:simpleType name="char">

      <xs:restriction base="xs:int"/>

  </xs:simpleType>

  <xs:element name="duration" nillable="true" type="tns:duration"/>

  <xs:simpleType name="duration">

      <xs:restriction base="xs:duration">

          <xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?"/>

              <xs:minInclusive value="-P10675199DT2H48M5.4775808S"/>

              <xs:maxInclusive value="P10675199DT2H48M5.4775807S"/>

      </xs:restriction>

  </xs:simpleType>

  <xs:element name="guid" nillable="true" type="tns:guid"/>

<xs:annotation>
  <xs:appInfo><blah:simpleType>blah blah blah</blah:simpleType></xs:appInfo>
  <xs:documentation xml:lang="en">
  3 blahs long!
  </xs:documentation>
</xs:annotation>
</xs:schema>

then the xpath would return both blah:simpleType and xs:simpleType,
but how often is that a real problem.

Cheers,
Bryan Rasmussen


More information about the erlang-questions mailing list