2

我正在尝试创建一个将返回所有子节点值的 xquery 表达式。现在我只得到每个孩子下的第一个子孩子。

我有以下 XML:

<order>
    <child1>
      <subchild id="S1">
        <name type="primary">
          <first>NAOMI</first>
          <middle />
          <last>ADAMS</last>
          <suffix />
        </name>
      </subchild>
      <subchild id="S2">
        <name type="primary">
          <first>TOVER</first>
          <middle />
          <last>DALI</last>
          <suffix />
        </name>
      </subchild>
    </child1>
    <child2>
      <subchild id="V1">
        <year>2002</year>
        <make>PONTI</make>
        <model>AZTEK</model>
        <vin>3G7DA03E32S597676</vin>
      </subchild>
      <subchild id="V2">
        <year>2003</year>
        <make>HONDA</make>
        <model>CIVIC</model>
        <vin>2H94DA03E80S1538</vin>
      </subchild>
    </child2>
    <child3>
      <subchild id="A1">
        <house>7741</house>
        <street1>SAINT BERNARD ST</street1>
        <apartment />
        <city>PLAYA DEL REY</city>
        <state>CA</state>
        <postalcode>90293</postalcode>
      </subchild>
      <subchild id="A2">
        <house>2371</house>
        <street1>HANNUM DR</street1>
        <apartment />
        <city>MARINA DEL REY</city>
        <state>CA</state>
        <postalcode>90293</postalcode>
      </subchild>
    </child3>
  </order>

我的结果应该是这样的:

  FirstName LastName    HouseNumber StreetName         City         SState  Zip    Year     Model   Make    VIN
   NAOMI     ADAMS        7741   SAINT BERNARD ST   PLAYA DEL REY     CA    90293   2002    PONTI   AZTEK   G7DA03E32S597676
   TOVER     DALI         2371    HANNUM DR         MARINA DEL REY    CA    90024   2003    HONDA   CIVIC   2H94DA03E80S1538

我的查询:

SELECT 
   FirstName, LastName, HouseNumber, StreetName, City, SState, Zip, Year, Model, Make, VIN
FROM 
   xmlTable 
OUTER APPLY 
(  
    SELECT  
       tbl.col.value('(child1/subchild/name/first)[1]','varchar(20)') AS FirstName,
       tbl.col.value('(child1/subchild/name/last)[1]','varchar(20)') AS LastName,
       tbl.col.value('(child1/subchild/name/middle)[1]','varchar(20)') AS MiddleName,
       tbl.col.value('(child3/subchild/house)[1]','varchar(20)') AS HouseNumber,
       tbl.col.value('(child3/subchild/street1)[1]','varchar(20)') AS StreetName,
       tbl.col.value('(child3/subchild/city)[1]','varchar(20)') AS City,
       tbl.col.value('(child3/subchild/state)[1]','varchar(20)') AS SState,
       tbl.col.value('(child3/subchild/postalcode)[1]','varchar(20)') AS Zip,
       tbl.col.value('(child2/subchild/model_year)[1]','varchar(20)') AS Year,
       tbl.col.value('(child2/subchild/model)[1]','varchar(20)') AS Model,
       tbl.col.value('(child2/subchild/make)[1]','varchar(20)') AS Make,
       tbl.col.value('(child2/subchild/vin)[1]','varchar(20)') AS VIN
    FROM 
       xmldocument.nodes('//order') AS tbl(col) 
   )  Y 

谢谢

4

1 回答 1

2
 
DECLARE @x XML 
SELECT @x = 'Your XML here'
SELECT   
tbl.col.value('(child1/subchild/name/first)[position() = sql:column("s.number")][1]','varchar(20)') AS FirstName, 
tbl.col.value('(child1/subchild/name/last)[position() = sql:column("s.number")][1]', 'varchar(20)') AS LastName, 
tbl.col.value('(child1/subchild/name/middle)[position() = sql:column("s.number")][1]', 'varchar(20)') AS MiddleName, 
tbl.col.value('(child3/subchild/house)[position() = sql:column("s.number")][1]', 'varchar(20)') AS HouseNumber, 
tbl.col.value('(child3/subchild/street1)[position() = sql:column("s.number")][1]', 'varchar(20)') AS StreetName, 
tbl.col.value('(child3/subchild/city)[position() = sql:column("s.number")][1]', 'varchar(20)') AS City, 
tbl.col.value('(child3/subchild/state)[position() = sql:column("s.number")][1]', 'varchar(20)') AS SState, 
tbl.col.value('(child3/subchild/postalcode)[position() = sql:column("s.number")][1]', varchar(20)') AS Zip, 
tbl.col.value('(child2/subchild/model_year)[position() = sql:column("s.number")][1]', 'varchar(20)') AS Year, 
tbl.col.value('(child2/subchild/model)[position() = sql:column("s.number")][1]', 'varchar(20)') AS Model, 
tbl.col.value('(child2/subchild/make)[position() = sql:column("s.number")][1]', 'varchar(20)') AS Make, 
tbl.col.value('(child2/subchild/vin)[position() = sql:column("s.number")][1]', 'varchar(20)') AS VIN 
FROM @x.nodes('/order') AS tbl(col) 
CROSS JOIN master..spt_values s  
WHERE type = 'P' AND number BETWEEN 1  
AND col.query('count(child1/subchild)').value('.', 'INT')
 
于 2011-05-20T22:59:43.770 回答