如何用xquery查询单个节点,使结果有不同的标签?
How to query a single node with xquery so that the result has a different tag?
如何 adjust 下面的 xquery
使得结果是:
<?xml version="1.0" encoding="UTF-8"?><Sex>F</Sex>
xml:
<csv>
<record>
<entry>Reported_Date</entry>
<entry>HA</entry>
<entry>Sex</entry>
<entry>Age_Group</entry>
<entry>Classification_Reported</entry>
</record>
<record>
<entry>2020-01-26</entry>
<entry>Vancouver Coastal</entry>
<entry>M</entry>
<entry>40-49</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-02</entry>
<entry>Vancouver Coastal</entry>
<entry>F</entry>
<entry>50-59</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-05</entry>
<entry>Vancouver Coastal</entry>
<entry>F</entry>
<entry>20-29</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-05</entry>
<entry>Vancouver Coastal</entry>
<entry>M</entry>
<entry>30-39</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-11</entry>
<entry>Interior</entry>
<entry>F</entry>
<entry>30-39</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-20</entry>
<entry>Fraser</entry>
<entry>F</entry>
<entry>30-39</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-21</entry>
<entry>Fraser</entry>
<entry>M</entry>
<entry>40-49</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-27</entry>
<entry>Vancouver Coastal</entry>
<entry>F</entry>
<entry>60-69</entry>
<entry>Lab-diagnosed</entry>
</record>
</csv>
xquery:
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
for $x in /csv
return $x/record[3]/entry[3]
结果:
<?xml version="1.0" encoding="UTF-8"?><entry>F</entry>
试试看
for $x in /csv
return
<Sex>
{$x/record[3]/entry[3]/text()}
</Sex>
输出:
<?xml version="1.0" encoding="UTF-8"?><Sex>F</Sex>
或者如果你像我一样不喜欢不必要的变量,你也可以这样做(在 3.1 中)
/csv/record[3]/entry[3] ! <Sex>{string(.)}</Sex>
如何 adjust 下面的 xquery
使得结果是:
<?xml version="1.0" encoding="UTF-8"?><Sex>F</Sex>
xml:
<csv>
<record>
<entry>Reported_Date</entry>
<entry>HA</entry>
<entry>Sex</entry>
<entry>Age_Group</entry>
<entry>Classification_Reported</entry>
</record>
<record>
<entry>2020-01-26</entry>
<entry>Vancouver Coastal</entry>
<entry>M</entry>
<entry>40-49</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-02</entry>
<entry>Vancouver Coastal</entry>
<entry>F</entry>
<entry>50-59</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-05</entry>
<entry>Vancouver Coastal</entry>
<entry>F</entry>
<entry>20-29</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-05</entry>
<entry>Vancouver Coastal</entry>
<entry>M</entry>
<entry>30-39</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-11</entry>
<entry>Interior</entry>
<entry>F</entry>
<entry>30-39</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-20</entry>
<entry>Fraser</entry>
<entry>F</entry>
<entry>30-39</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-21</entry>
<entry>Fraser</entry>
<entry>M</entry>
<entry>40-49</entry>
<entry>Lab-diagnosed</entry>
</record>
<record>
<entry>2020-02-27</entry>
<entry>Vancouver Coastal</entry>
<entry>F</entry>
<entry>60-69</entry>
<entry>Lab-diagnosed</entry>
</record>
</csv>
xquery:
declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
for $x in /csv
return $x/record[3]/entry[3]
结果:
<?xml version="1.0" encoding="UTF-8"?><entry>F</entry>
试试看
for $x in /csv
return
<Sex>
{$x/record[3]/entry[3]/text()}
</Sex>
输出:
<?xml version="1.0" encoding="UTF-8"?><Sex>F</Sex>
或者如果你像我一样不喜欢不必要的变量,你也可以这样做(在 3.1 中)
/csv/record[3]/entry[3] ! <Sex>{string(.)}</Sex>