Project

Profile

Help

Namespaces in imported modules

Added by Anonymous over 13 years ago

Legacy ID: #8773105 Legacy Poster: ldueck (ldueck)

I have a library module that looks like this: Module namespace lib = “http://mylibrary”; declare function lib:createElement( $tmpname as xs:string, $value as element()) as element() { let $elementname := $tmpname where (exists($value)) return element {$elementname} { data($value) } }; I import that module and use that generic function from many different main queries. For example in my main query... Let $elementname := “book” Return {$elementname} { lib:createElement(‘author’, $elementA), lib:createElement(‘title’, $elementB), lib:createElement(‘publisher’, $elementC), ... } returns me janedoe <title>merrychristmas</title> scholastic This works fine, however I run into an issue with namespaces. Each main query that uses this library function, needs to create elements with different namespaces. What I’d like to know is how I use a generic function like this, to produce an element with a namespace, without having to edit the library file and add the applicable namespace declaration? For example, if I want my result to be the following... indigo:book indigo:authorJaneDoe</indigo:author> indigo:titleMerryChristmas</indigo:title> indigo:publisherACME</indigo:publisher> </indigo:book> The only way I can get it to work right now is to include the “indigo” namespace declaration in the library module, and append “indigo:” to the start of “book”, “author” “title” and “publisher” strings. The library module shouldn’t have to be edited by every developer who wants to use the generic function with a namespace. I have tried editing the library function to add a namespace as an attribute but that didn’t work. Ie declare function lib:createElementWithNamespace( $elementname as xs:string, $namespacePrefix as xs:string, (“indigo”) $namespaceValue as xs:string, (“http://xmlns.acme.com/indigo”) $elementvalue as element()) as element() { let $attrName := concat("xmlns:", $namespacePrefix) let $elem := element{$elementname}{ attribute{$attrName}{data($namespaceValue)}, data($elementvalue) } return $elem }; Any suggestions would be greatly appreciated,


Replies (1)

RE: Namespaces in imported modules - Added by Anonymous over 13 years ago

Legacy ID: #8773151 Legacy Poster: Michael Kay (mhkay)

The simplest approach is to use your existing function, but making $tmpname an xs:QName rather than an xs:string. That's the only change needed to your function - but it puts the onus of creating the QName on the caller. If you prefer to accept prefix, localname, and namespace URI as separate parameters, then you can construct the xs:QName yourself using the fn:QName function.

    (1-1/1)

    Please register to reply