Frage

I'm attempting to create XQuery code which constructs a namespaced xml element and inserts into it the text from an existing (non-namespaced) element. The following code is a simplified example:

xquery version "1.0-ml";

let $xml := <wrapper><elem>textIWant</elem></wrapper>
return <newElem xmlns="http://www.blah.org">{$xml/elem/text()}</newElem>

Instead of producing the desired output:

<newElem xmlns="http://www.blah.org">textIWant</newElem>

It produces this:

<newElem xmlns="http://www.blah.org"/>

This code, however, works:

xquery version "1.0-ml";

let $xml := <wrapper><elem>textIWant</elem></wrapper>
let $elemText := $xml/elem/text()
return <newElem xmlns="http://www.blah.org">{$elemText}</newElem>

Using the other form of xml construction (element () {...}) also works fine. The issue only seems to happen when the xml being constructed is being given a namespace. What's the problem with the first code snippet?

War es hilfreich?

Lösung

Providing a namespace via the xmlns pseudo-attribute changes the default element namespace in the nested XQuery scope. You can get around this by using namespace prefixes,

declare namespace ns = "http://www.blah.org";
let $xml := <wrapper><elem>textIWant</elem></wrapper>
return <ns:newElem>{ $xml/elem/text() }</ns:newElem>

or using the "any namespace" XPath syntax:

$xml/*:elem/text()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top