同一xml文件的多个属性

时间:2012-02-27 10:17:43

标签: xml xquery

XQuery脚本需要显示xml文件中的所有公司名称和联系人姓名。

这就是我所拥有的: -

for $x in doc("Customers.xml")/dataroot/Customers return $x/CompanyName $x/ContactName

示例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>

我想仅返回CompanyName和ContactName。这些必须能够轻松转换为表格格式,因此它以列

结构化

1 个答案:

答案 0 :(得分:1)

XQuery的基本数据模型是sequence,而不是表格或relation。这也适用于XQuery的result。因此,默认情况下,您将收到return子句返回的序列。默认情况下不返回“列”或“表”。如果你需要一个不同的或“更深层次结构”的返回结构,那么你必须自己添加它。

获取结构化结果的最简单方法是创建XML输出。因此,大多数XQuery教程都以返回XML开始,而不是纯文本或其他任何内容。

如果您需要“表格结构”作为输出,则有不同的选项:

  • 输出XML并将其导入另一个应用程序;
  • 输出(X)带HTML表格的HTML;
  • 输出CSV并使用您首选的电子表格软件导入/解析它。

要输出XML,您可以使用以下内容:

xquery version "1.0";
<table>
{
    for $cust in fn:doc("Customers.xml")/dataroot/Customers
    return
        <row>
        {
            $cust/CompanyName, $cust/ContactName
        }
        </row>
}
</table>

它会返回类似的内容:

<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>

对于CSV输出(更准确地说:一系列类似CSV的字符串),您可以使用

xquery version "1.0";
for $cust in fn:doc("Customers.xml")/dataroot/Customers
return
concat('"', $cust/CompanyName, '"', ',', '"', $cust/ContactName, '"')

获得类似

的内容
"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"

编辑:一些XQuery处理器也提供内置的CSV序列化。例如,Zorba XQuery processor包括module to parse / serialize CSV