回答編集履歴
2
a
answer
CHANGED
@@ -1,3 +1,37 @@
|
|
1
1
|
- [XQuery looking for text with 'single' quote](http://stackoverflow.com/questions/13482352/xquery-looking-for-text-with-single-quote)
|
2
2
|
|
3
|
-
リテラルを分割してXPathの`concat`関数で結合する,という方法があるようです.但し分割パートが1つとなってしまった場合にエラーになるので,`concat`関数の引数が必ず2つ以上になるように空文字列のリテラルを渡す必要があります.
|
3
|
+
リテラルを分割してXPathの`concat`関数で結合する,という方法があるようです.但し分割パートが1つとなってしまった場合にエラーになるので,`concat`関数の引数が必ず2つ以上になるように空文字列のリテラルを渡す必要があります.
|
4
|
+
|
5
|
+
```html
|
6
|
+
<ul id="sample">
|
7
|
+
<li>test</li>
|
8
|
+
<li>"hoge"</li>
|
9
|
+
<li>'foo'</li>
|
10
|
+
<li>"piyo'</li>
|
11
|
+
</ul>
|
12
|
+
<script>
|
13
|
+
'use strict';
|
14
|
+
|
15
|
+
function xPathLiteralize(str) {
|
16
|
+
var r = "concat('" + String(str).replace(/'/g, "', \"'\", '") + "', '')";
|
17
|
+
console.log(r); // just for debugging
|
18
|
+
return r;
|
19
|
+
}
|
20
|
+
|
21
|
+
function getFirstTextNode (contextNode, string) {
|
22
|
+
return document.evaluate(
|
23
|
+
'descendant::text()[.=' + xPathLiteralize(string) + ']',
|
24
|
+
ul,
|
25
|
+
null,
|
26
|
+
XPathResult.FIRST_ORDERED_NODE_TYPE,
|
27
|
+
null
|
28
|
+
).singleNodeValue;
|
29
|
+
}
|
30
|
+
|
31
|
+
var ul = document.getElementById('sample');
|
32
|
+
console.log(getFirstTextNode(ul, 'test'));
|
33
|
+
console.log(getFirstTextNode(ul, '"hoge"'));
|
34
|
+
console.log(getFirstTextNode(ul, "'foo'"));
|
35
|
+
console.log(getFirstTextNode(ul, '"piyo\''));
|
36
|
+
</script>
|
37
|
+
```
|
1
a
answer
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
- [XQuery looking for text with 'single' quote](http://stackoverflow.com/questions/13482352/xquery-looking-for-text-with-single-quote)
|
2
2
|
|
3
|
-
リテラルを分割してXPathの`concat`関数で結合する,という方法があるようです.
|
3
|
+
リテラルを分割してXPathの`concat`関数で結合する,という方法があるようです.但し分割パートが1つとなってしまった場合にエラーになるので,`concat`関数の引数が必ず2つ以上になるように空文字列のリテラルを渡す必要があります.
|