[英]Multiple attributes of the same xml file
The XQuery script needs to display all Company Name and Contact Name that is in the xml file. XQuery脚本需要显示xml文件中的所有公司名称和联系人名称。
This is what I have:- 这就是我所拥有的:
for $x in doc("Customers.xml")/dataroot/Customers return $x/CompanyName $x/ContactName
Example xml 范例xml
<dataroot>
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
<Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitución 2222</Address>
<City>México D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
<Customers>
<CustomerID>ANTON</CustomerID>
<CompanyName>Antonio Moreno Taquería</CompanyName>
<ContactName>Antonio Moreno</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Mataderos 2312</Address>
<City>México D.F.</City>
<PostalCode>05023</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-3932</Phone>
</Customers>
<Customers>
<CustomerID>AROUT</CustomerID>
<CompanyName>Around the Horn</CompanyName>
<ContactName>Thomas Hardy</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>120 Hanover Sq.</Address>
<City>London</City>
<PostalCode>WA1 1DP</PostalCode>
<Country>UK</Country>
<Phone>(171) 555-7788</Phone>
<Fax>(171) 555-6750</Fax>
</Customers>
<Customers>
<CustomerID>BERGS</CustomerID>
<CompanyName>Berglunds snabbköp</CompanyName>
<ContactName>Christina Berglund</ContactName>
<ContactTitle>Order Administrator</ContactTitle>
<Address>Berguvsvägen 8</Address>
<City>Luleå</City>
<PostalCode>S-958 22</PostalCode>
<Country>Sweden</Country>
<Phone>0921-12 34 65</Phone>
<Fax>0921-12 34 67</Fax>
</Customers>
I want to return CompanyName and ContactName ONLY. 我只想返回CompanyName和ContactName。 These must be able to easily be converted into a table format so it is structured in columns
这些必须能够轻松转换为表格格式,以便按列进行结构化
XQuery's basic data model is the sequence , not the table or the relation . XQuery的基本数据模型是序列 ,而不是表或关系 。 This applies to the
result
of a XQuery, too. 这也适用于XQuery的
result
。 So, by default you will receive a sequence returned by the return
clause. 因此,默认情况下,您将收到
return
子句返回的序列。 There are no "columns" or "tables" to return by default. 默认没有返回的“列”或“表”。 If you need a different or "deeper-structured" return structure other then sequence, you will have to add it yourself.
如果您需要序列以外的其他或“更深层次的”返回结构,则必须自己添加。
The easiest way to get a structured result is to create XML output. 获得结构化结果的最简单方法是创建XML输出。 Because of that, most XQuery tutorials start with returning XML, not plain text or anything else.
因此,大多数XQuery教程都以返回XML开头,而不是纯文本或其他任何东西。
If you need a "table structure" as output, you have different options: 如果需要“表结构”作为输出,则有不同的选择:
To output XML, you could use the following: 要输出XML,可以使用以下命令:
xquery version "1.0";
<table>
{
for $cust in fn:doc("Customers.xml")/dataroot/Customers
return
<row>
{
$cust/CompanyName, $cust/ContactName
}
</row>
}
</table>
It will return something like: 它将返回类似:
<table>
<row>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
</row>
<row>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
</row>
<row>
<CompanyName>Antonio Moreno Taquería</CompanyName>
<ContactName>Antonio Moreno</ContactName>
</row>
<row>
<CompanyName>Around the Horn</CompanyName>
<ContactName>Thomas Hardy</ContactName>
</row>
<row>
<CompanyName>Berglunds snabbköp</CompanyName>
<ContactName>Christina Berglund</ContactName>
</row>
</table>
For CSV output (to be more precise: a sequence of CSV-like strings), you could use 对于CSV输出(更精确地说:一系列类似CSV的字符串),您可以使用
xquery version "1.0";
for $cust in fn:doc("Customers.xml")/dataroot/Customers
return
concat('"', $cust/CompanyName, '"', ',', '"', $cust/ContactName, '"')
to get something like 得到像
"Alfreds Futterkiste","Maria Anders"
"Ana Trujillo Emparedados y helados","Ana Trujillo"
"Antonio Moreno Taquería","Antonio Moreno"
"Around the Horn","Thomas Hardy"
"Berglunds snabbköp","Christina Berglund"
EDIT: Some XQuery processors offer built-in CSV serialisation, too. 编辑:一些XQuery处理器也提供内置的CSV序列化。 For example, th Zorba XQuery processor includes a module to parse / serialize CSV .
例如, Zorba XQuery处理器包括一个用于解析/序列化CSV的模块 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.