When should I use /text() at the end of my XPath expression?
2011-12-08 11:11
483 查看
You have probably seen a lot of examples that use
/text() at the end of XPath expressions, and you might be wondering, Should I do the same? Are there any drawbacks?
There are cases in which you do want to append
/text() to your XPath expression to force your XQuery to consider the text value of your XPath expression; the typical case is when you want to dynamically create the value of a result element; something like this:
<title>{$doc//book[1]/book-title}</title>
… generates this output:
<title>
<book-title>La Divina Commendia</book-title>
</title>
However, if what you are trying to create is actually this:
<title>La Divina Commendia</title>
… then you need to instruct XQuery to use the text value of the
<book-title> element, and an easy way to do that is appending
/text() to the XPath expression:
<title>{$doc//book[1]/book-title/text()}</title>
But often XQuery developers use
/text() too frequently, in cases similar to this, for example:
for $book in $doc//book[title/text()
= "La Divina Commendia"]
return …
What are the drawbacks of using
/text() when it is not needed? It turns out that there are several:
If the data is typed, the type information is thrown away. Beside the possibly unnecessary type conversions, this also makes the query less type safe. This can also cause significant performance disadvantages when dealing with relational data sources.
It causes the query to fail — or even produce wrong results — if the element contains comment nodes. There are cases in which this is not true, but in general queries should be written so they aren't affected by comments nodes. Consider the following XQuery,
for example:
let $doc
:= <root><val>foo<!-- comment -->bar</val></root>
return
($doc//val = "foobar",
$doc//val/text() = "foobar")
It returns (true, false) because the result of
$doc//val/text() is actually the sequence (“foo”, “bar”) and not “foobar” as you would expect.
It makes your queries more verbose than needed
/text() at the end of XPath expressions, and you might be wondering, Should I do the same? Are there any drawbacks?
There are cases in which you do want to append
/text() to your XPath expression to force your XQuery to consider the text value of your XPath expression; the typical case is when you want to dynamically create the value of a result element; something like this:
<title>{$doc//book[1]/book-title}</title>
… generates this output:
<title>
<book-title>La Divina Commendia</book-title>
</title>
However, if what you are trying to create is actually this:
<title>La Divina Commendia</title>
… then you need to instruct XQuery to use the text value of the
<book-title> element, and an easy way to do that is appending
/text() to the XPath expression:
<title>{$doc//book[1]/book-title/text()}</title>
But often XQuery developers use
/text() too frequently, in cases similar to this, for example:
for $book in $doc//book[title/text()
= "La Divina Commendia"]
return …
What are the drawbacks of using
/text() when it is not needed? It turns out that there are several:
If the data is typed, the type information is thrown away. Beside the possibly unnecessary type conversions, this also makes the query less type safe. This can also cause significant performance disadvantages when dealing with relational data sources.
It causes the query to fail — or even produce wrong results — if the element contains comment nodes. There are cases in which this is not true, but in general queries should be written so they aren't affected by comments nodes. Consider the following XQuery,
for example:
let $doc
:= <root><val>foo<!-- comment -->bar</val></root>
return
($doc//val = "foobar",
$doc//val/text() = "foobar")
It returns (true, false) because the result of
$doc//val/text() is actually the sequence (“foo”, “bar”) and not “foobar” as you would expect.
It makes your queries more verbose than needed
相关文章推荐
- Why do my text files suddenly have an additional at the end of each line?
- Linux - How to remove the new line at the end of file when using vim
- some issues that you should be take care of when use the plupload module
- 【android】Android Studio2.3打包提示Please select at least one of the signature versions to use
- Application windows are expected to have a root view controller at the end of application launch
- main.c :10:2 warning: no newline at the end of file
- error -27257: Pending web_reg_save_param/reg_find/create_html_param[_ex] request(s) detected and reset at the end of iteration number 1
- Programming Basic--what's the use of "ifndef/define/endif";
- [Forward]Use the SharePoint My Tasks Web Part outside of My Sites
- by the end of, at the end of , in the end , to the end 的区别与用法
- ios6.0之后单个屏幕旋转,不用根视图以及Application windows are expected to have a root view controller at the end of
- Application windows are expected to have a root view controller at the end of application launch'
- Application windows are expected to have a root view controller at the end of appliaction launch.
- Why did the size of my indexes expand when I rebuilt my indexes?
- The end of my freshman study...
- Use #CSS attribute selectors to display links when <a> has no text value but the `href` has a link
- Picture&nbsp;of&nbsp;my&nbsp;baby&nbsp;when&nbsp;2&nbsp;monthe…
- “Application windows are expected to have a root view controller at the end of application launch”
- When I use the Report Viewer ,I have the error,how should i do?
- firefox报错 关于 JSON.parse: unexpected end of data at line 1 column 1 of the JSON data