[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

fn:in-scope-prefixes

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


  fn:in-scope-prefixes
> 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                       http://x-query.com/mailman/listinfo/talk
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)


Purchase Stylus Studio Online Today!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.