Can't iterate across xlink:href attribute

Issues related to W3C XQuery.
Doug
Posts: 28

Can't iterate across xlink:href attribute

Wed May 27, 2015 2:22 am

I get an error "Undeclared prefix in attribute name: xlink when I try to iterate across an xlink:href attribute and my attempts to declare the prefix either is ineffective or produces the error: "Evaluation will always throw a dynamic error: Namespace prefix" is invalid: xmlns:xlink".
I am trying to sort bibliomixed elements in bibliography in a docbook variant.

I'm using Oxygen 17.

My xquery, with one version of the attempt to declare the xlink namespace to avoid the first error, is below. (Note this code works without the xlink:href attribute on the bibliography element and without the namespace declaration. And the truth is I really don't need this attribute in that place but I do need the links on each bibliomixed element. Another problem I have is that without a declaration on the bibliography element I get a declaration on every bibliomixed element which I don't want.):

Code: Select all

xquery version "3.0";

declare default element namespace "http://docbook.org/ns/docbook";

declare variable $currentFile external;

let $doc := doc($currentFile)
let $bibliography := $doc/bibliography
let $new-order := (for  $bib in $bibliography/bibliomixed
order by  $bib//author[1]/personname/surname, $bib/pubdate[1], $bib/title[1]
  return $bib )
 
let $new-bibliography := element bibliography {
   namespace {"xmlns:xlink"} {"http://www.w3.org/1999/xlink"},
   for $attr in $bibliography/@*
    return
     attribute {name($attr)} {$attr},
    $new-order
  }
 
 return
   $new-bibliography



My simplified and abbreviated xml document code which does validate is:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="file:///C:/hraf.docbook/5.0-variant-HRAFCollectionInfo/rng/hraf.docbook.rnc" type="application/relax-ng-compact-syntax"?>
<?xml-stylesheet "text/css" href="file:///C:/frameworks/hraf.docbook/css/hraf.docbook.css"?>
<bibliography xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://docbook.org/ns/docbook"
   hrafid="this" ocms="113" xlink:href="http://hraf.org">
   <info>
      <title>BIBLIOGRAPHY</title>
   </info>
   <bibliomixed xlink:href="http://ehrafarchaeology.yale.edu/document?id=se49-003" xlink:show="new"
      xml:id="Burger1985TheEarlyCeremonialCenterofHuaricoto">
      <info>
         <bibliorelation xlink:href="owc/se43"/>
      </info>
      <authorgroup>
         <author>
            <personname>
               <surname>Burger</surname>, <firstname> Richard</firstname>
            </personname>
         </author> and <author>
            <personname>
               <firstname>Lucy</firstname>
               <surname>Salazar-Burger</surname>
            </personname>
         </author>
      </authorgroup> (<pubdate>1985</pubdate>). "<title>The Early Ceremonial Center of
         Huaricoto</title>." In <title>
         <emphasis>Early Ceremonial Architecture in the Andes: a Conference at Dumbarton Oaks, 8th
            to 10th October 1982</emphasis>
      </title>, <editor>
         <personname>
            <firstname>Christopher B.</firstname>
            <surname>Donnan</surname>
         </personname>
      </editor>, ed. <publisher>
         <publishername>Washington, D.C.: Dumbarton Oaks Research Library and
            Collection</publishername>
      </publisher>.</bibliomixed>
...
</bibliography>
Radu
Posts: 5088

Re: Can't iterate across xlink:href attribute

Wed May 27, 2015 3:55 pm

Hi Doug,

We do not work very much with XQuery ourselves. So ideally you could try to find a specialized users list for this.
Why don't you declare the xlink prefix at the beginning of the XQuery file?
Like:

Code: Select all

xquery version "3.0";

declare default element namespace "http://docbook.org/ns/docbook";
declare namespace xlink = "http://www.w3.org/1999/xlink";

declare variable $currentFile external;

let $doc := doc($currentFile)
let $bibliography := $doc/bibliography
let $new-order := (for  $bib in $bibliography/bibliomixed
order by  $bib//author[1]/personname/surname, $bib/pubdate[1], $bib/title[1]
  return $bib )
 
let $new-bibliography := element bibliography {
   for $attr in $bibliography/@*
    return
     attribute {name($attr)} {$attr},
    $new-order
  }
 
 return
   $new-bibliography


Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Doug
Posts: 28

Re: Can't iterate across xlink:href attribute

Wed May 27, 2015 4:55 pm

Thanks Radu. Declaring the xlink namespace at the top of the xquery file, same as the docbook namespace, solved my problem. That was the kind of stupid oversight that I fear and sometimes makes me reluctant to ask for help. Still leaves a question as to how to declare such when using an element constructor, but I don't need that now.

The general problem for me is that it still seems mysterious how namespace declarations get propagated by engines like Saxon in document results from either the original document being transformed or from the transformation query. Without the xlink declaration at the top of the xquery file the xlink namepace proclamations were getting propagated to every bibliomixed element that used the xlink:href attribute, I assume from the original document going into the transformation. I tried to replicate this behavior by putting an xlink:href attribute on the bilbiliography element so the namespace declaration would propagate to it instead of every child bibliomixed element. But iterating across the bibliography element didn't work without a declaration in the xquery file, unlike with simply outputting the whole bibliomixed elements, which propagated the namespace declaration without it being in the xquery file. Then I couldn't declare the xlink namespace in the element constructor, and completly missed the parallel with the docbook declaration at the top of the file, which I put there so I didn't have to use prefixes in my element names.

As for using a more specifically dedicated xquery list, I will do so when my time for understanding the above issues matches my curiousity. In the meantime you do have an xquery topic group on the forum which I used, I find Oxygen an excellent xquery IDE, and Saxon an excellent xquery engine, and most importantly, you solved my problem in quick order. Thanks again.
Radu
Posts: 5088

Re: Can't iterate across xlink:href attribute

Thu May 28, 2015 9:19 am

Hi Doug,

No problem, I think XQuery is similar to XSLT in this behavior.
When you copy entire chunks of XML content from the original document to the output the namespace declarations in the original document get copied to the output.
But when in the XQuery or XSLT you run an XPath you need to declare in that stylesheet/xquery document the prefixes which are used in the XPath. And the same seems to go for using the name function in your sample XQuery. So the processor does not learn the prefix-namespace mappings from the parsed XML document.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “XQuery”

Who is online

Users browsing this forum: No registered users and 0 guests