#xml #xquery
#xml #xquery
Вопрос:
У меня есть сомнения с кодом XQuery, который я написал для проекта uni. Код выглядит следующим образом:
<results>
{
for $item in doc("data.xml")//item
return
<country alpha-2="{$item/city/country}">
<name> {doc("countries.xml")//country[@alpha-2=$item/city/country]/@name} </name>
<cities>
<city>
<name> {$item/city/@name} </name>
<temp unit="{$item/temperature/@unit}"> {$item/temperature/@value} </temp>
<feels_like unit="{$item/feels_like/@unit}"> {$item/feels_like/@value} </feels_like>
<humidity unit="{$item/humidity/@unit}"> {$item/humidity/@value} </humidity>
<pressure unit="{$item/pressure/@unit}"> {$item/pressure/@value} </pressure>
<clouds> {$item/clouds/@name} </clouds>
</city>
</cities>
</country>
}
</results>
Это кажется довольно простым, но когда я его компилирую, значения новых узлов отображаются в виде атрибутов, как показано в следующем коде:
<?xml version="1.0" encoding="UTF-8"?>
<results>
<country alpha-2="LV">
<name name="Latvia"/>
<cities>
<city>
<name name="Zilupe"/>
<temp unit="kelvin" value="281.15"/>
<feels_like unit="kelvin" value="277.59"/>
<humidity unit="%" value="86"/>
<pressure unit="hPa" value="1023"/>
<clouds name="overcast clouds"/>
</city>
</cities>
</country>
<country alpha-2="RU">
<name name="Russian Federation"/>
<cities>
<city>
<name name="Sebezh"/>
<temp unit="kelvin" value="280.82"/>
<feels_like unit="kelvin" value="277.37"/>
<humidity unit="%" value="90"/>
<pressure unit="hPa" value="1023"/>
<clouds name="overcast clouds"/>
</city>
</cities>
</country>
</results>
Кто-нибудь знает, почему это происходит? Вероятно, я упускаю что-то очевидное, но я этого не вижу.
Комментарии:
1. Пожалуйста, отредактируйте свой пост и добавьте желаемый результат.
Ответ №1:
Поскольку вы извлекаете атрибут из xpath, то это то, что вы помещаете в элемент. Вам нужно окружить xpath символом xs:string()
<name> {doc("countries.xml")//country[@alpha-2=$item/city/country]/@name} </name>
Должно быть
<name> {xs:string(doc("countries.xml")//country[@alpha-2=$item/city/country]/@name)} </name>