[xquery-talk] fn:in-scope-prefixes

James A. Robinson jim.robinson at stanford.edu
Fri Mar 2 10:56:55 PST 2007


> On Thursday 15 February 2007 05:39, James A. Robinson wrote:
> 
> Could you show the code that produced it, by any chance?

I'd be happy to have someone look at it and tell me if it is flawed.
Here is the function:

declare function f:namespace-fixup($e as element())
as attribute()*
{
  for $p in in-scope-prefixes($e)
  where ($p ne '')
  return (
    attribute {QName(namespace-uri-for-prefix($p, $e), 'x')} {}
  )
};

I am using it in a function where I have take an element which may have
attribute *values* which are QNames with namespace prefixes, and I need
to make sure the namespaces for those values are copied.  So something
like the following:

declare function f:inline-expand($uri as xs:anyURI)
as element()?
{
  if (doc-available($uri))
  then (
    let $root := doc($uri)/*
    return (
      element {'namespace-fixup'} {
        f:namespace-fixup($root),
        element {node-name($root)} {
          $root/@*,
          for $node in $root/node()
          return (
            if ($node[self::link])
            then (
              f:inline-expand($node/@href)
            )
            else $node
          )
        }
      }/*
    )
  )
  else ()
};

If, for example, you had a pair of documents

doc1.xml:

<root
  xmlns:n1="uri.n1"
  xmlns:n2="uri.n2"
  attr="n1:bar">
  <link href="doc2.xml"/>
</root>

doc2.xml:

<root
  xmlns:n3="uri.n3"
  xmlns:n4="uri.n4"
  attr="n4:bar">
</root>

and were not using the namespace fixup routine, a straight expansion
and copy of the element nodes and attributes would result in:

<?xml version="1.0" encoding="UTF-8"?>
<root attr="n1:bar">
  <root attr="n4:bar">
  </root>
</root>

instead of including the required n1 and n4 namespace declarations:

<root xmlns:n2="uri.n2" xmlns:n1="uri.n1" attr="n1:bar">
  <root xmlns:n3="uri.n3" xmlns:n4="uri.n4" attr="n4:bar">
  </root>
</root>

I was trying to solve the problem using techniques discussed on other
lists, and this one seemed like the best one since it did not require
me to use a product-specific extension.


Jim

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       jim.robinson at stanford.edu
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)


More information about the talk mailing list