Получить все значения Subchild, используя XQUERY-SQL - PullRequest
2 голосов
/ 19 мая 2011

Я пытаюсь создать выражение 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 

Спасибо

1 Ответ

2 голосов
/ 21 мая 2011
 
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')
 
...