Friday, January 6, 2012

Gotchas with XQuery: 4 cases

1. In Xquery (and also XSLT) once you defined a variable you can't change its value. This is due to the properties of declarative programming languages. A common workaround is by redefining the variable.

2. A boolean variable can't be compared with a string 'true' (or 'false')
e.g. if ($aboolvariable='true') will not work
solution: if ($aboolvariable= xs:boolean('true'))

3. If you use if you need to use else even if it's empty
e.g. if ($theanswer='true') then 'the answer is true' else ()

4.. Computed constructor element and the attribute string
Given <media type="book" title="Improve your intelligence for dummies"/>, suppose you want to construct an xml element using: element {$media/@type}{$media/@title}. Recall that the definition of the computed constructor element {name}{content}, so we expect to get: <book> Improve your intelligence for dummies </book > but instead we get: <book title="Improve your intelligence for dummies" />. To solve this problem use: element {$media/@type}{ string($media/@title)}.
Source: Steve's blogs

Any comments are welcome :)

Common Xquery mistakes by James Fuller

No comments: