0

I have the following issue with my local function.

The following function:

declare function local:exp($w as node()) as element()* {
 for $e in ($w/e)
 let $exp:= QName ("myns", "real")
 return 
  element {$exp}{ 
   attribute resource {$e/@lang}
  }
};

generates this xml:

<real xmlns="myns" resource="eng"/>

What really needed is:

<myns:real rdf:resource="lang"/>

How can I achive that?

  1. How can I address the problem?
  2. How can I add "rdf" as NS for resource attribute.

Thanks in advance.

1 Answer 1

1

You can assign the prefix to the QName as so:

let $exp:= QName ("urn:my-namespace", "myns:real")

Probably the best way to solve this is to declare these namespaces in your query and just refer to them by prefix:

declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare namespace myns="urn:my-namespace";

declare function local:exp($w as node()) as element()* {
 for $e in $w/e
 return 
  element myns:real { 
   attribute rdf:resource {$e/@lang}
  }
};

Note that you can simplify your function by using direct constructors:

declare function local:exp($w as node()) as element()* {
 for $e in $w/e
 return <myns:real rdf:resource="{$e/@lang}" />
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer. Your solution does partially work. However the result becomes like: <myns:real rdf:resource="en" xmlns:myns="urn:my-namespace" xmlns:rdf="w3.org/1999/02/22-rdf-syntax-ns#" />. So how can I remove this? I declared different variants of copy-namespaces and that does not help.
Ok. It must have been oXygen that was caching my queries. After having changed the structure of the result, it now takes into account copy-namespace declaration and the NS declaration is removed from the results. Thanks again.
@user322034: It should not be possible to use a namespace prefix without having the namespace in the document. The best you can do is to ensure that the top level element in the document has these prefixes (which it will do if it is created in the same module as the declare namespace declarations), and then they will appear at the top of the document rather than on every myns:real element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.