2017-07-17 11 views
2

挿入し、複雑なXML

<Documents> 
    <Batch BatchID="1" BatchName="Fred Flintstone"> 
    <DocCollection> 
     <Document DocumentID="269" FileName="CoverPageInstructions.xsl"> 
     <MergeFields> 
      <MergeField FieldName="A" Value="" /> 
      <MergeField FieldName="B" Value="" /> 
      <MergeField FieldName="C" Value="" /> 
      <MergeField FieldName="D" Value="" /> 
     </MergeFields> 
     </Document> 
     <Document DocumentID="6" FileName="USform8802.pdf"> 
     <MergeFields> 
      <MergeField FieldName="E" Value="" /> 
      <MergeField FieldName="F" Value="" /> 
      <MergeField FieldName="G" Value="" /> 
      <MergeField FieldName="H" Value="" /> 
      <MergeField FieldName="I" Value="" /> 
     </MergeFields> 
     </Document> 
     <Document DocumentID="299" FileName="POASIDE.TIF"> 
     <MergeFields /> 
     </Document> 
    </DocCollection> 
    </Batch> 
    <Batch BatchID="2" BatchName="Barney Rubble"> 
    <DocCollection> 
     <Document DocumentID="269" FileName="CoverPageInstructions.xsl"> 
     <MergeFields> 
      <MergeField FieldName="A" Value="" /> 
      <MergeField FieldName="B" Value="" /> 
      <MergeField FieldName="C" Value="" /> 
      <MergeField FieldName="D" Value="" /> 
     </MergeFields> 
     </Document> 
     <Document DocumentID="6" FileName="USform8802.pdf"> 
     <MergeFields> 
      <MergeField FieldName="E" Value="" /> 
      <MergeField FieldName="F" Value="" /> 
      <MergeField FieldName="G" Value="" /> 
      <MergeField FieldName="H" Value="" /> 
      <MergeField FieldName="I" Value="" /> 
     </MergeFields> 
     </Document> 
    </DocCollection> 
    </Batch> 
</Documents> 

私はこの結果を達成しようとしている:

BatchID BatchName DocumentID FieldName 
1 Fred Flintstone 269 A 
1 Fred Flintstone 269 B 
1 Fred Flintstone 269 C 
1 Fred Flintstone 269 D 
1 Fred Flintstone 6 E 
1 Fred Flintstone 6 F 
1 Fred Flintstone 6 G 
1 Fred Flintstone 6 H 
1 Fred Flintstone 6 I 
1 Fred Flintstone 299 Null 
2 Barney Rubble 269 A 
2 Barney Rubble 269 B 
2 Barney Rubble 269 C 
2 Barney Rubble 269 D 
2 Barney Rubble 6 E 
2 Barney Rubble 6 F 
2 Barney Rubble 6 G 
2 Barney Rubble 6 H 
2 Barney Rubble 6 I 

私はこの(各ドキュメントを登録しようデカルトを取得しているようです

SELECT lvl1.n.value('@BatchID','int'), 
     lvl1.n.value('@BatchName','varchar(50)'), 
     lvl2.n.value('@DocumentID','int'), 
     lvl3.n.value('@FieldName','varchar(50)') 
FROM @Data.nodes('Documents/*') lvl1(n) 
CROSS APPLY lvl1.n.nodes('DocCollection/Document') lvl2(n) 
CROSS APPLY lvl1.n.nodes('DocCollection/Document/MergeFields/MergeField') lvl3(n) 

そして、私が必要とする結果を得る方法が不思議です.Null va MergeField要素を持たないバッチ1の299 DocumentIDのlue。

何か助けていただければ幸いです。

おかげ

カール

UPDATE:ここでは同じ使用のOpenXMLを行う方法は次のとおりです。

SELECT 
    BatchID, 
    BatchName, 
    DocumentID, 
    FileName, 
    KeyData, 
    FieldName 
    FROM OPENXML(@hdoc, '/Documents/Batch/DocCollection/Document/MergeFields/MergeField', 11) 
     WITH (BatchID varchar(100) '../../../../@BatchID', 
      BatchName varchar(100) '../../../../@BatchName', 
      DocumentID varchar(100) '../../@DocumentID', 
      FileName varchar(100) '../../@FileName', 
      KeyData varchar(100) '../../@KeyData', 
      FieldName varchar(100) '@FieldName'); 

答えて

3

編集 - 申し訳ありませんが、私はフレッドにNULLを逃しました。ただ、クロスが適用...他のクロスを適用外に の適用に変更することで、各フィールドのエイリアスを作成していない理由を

お知らせLvl3

ない明確な必要に応じて外部の適用可能性があります。例えば、私が何か

SELECT lvl1.n.value('@BatchID','int'), lvl1.n.value('@BatchName','varchar(50)'), lvl2.n.value('@DocumentID','int'), lvl3.n.value('@FieldName','varchar(50)') FROM @Data.nodes('Documents/Batch') lvl1(n) CROSS APPLY lvl1.n.nodes('DocCollection/Document') lvl2(n) Outer APPLY lvl2.n.nodes('MergeFields/MergeField') lvl3(n) 

BatchID = lvl1.n.value('@BatchID','int'),のような戻り値

enter image description here

+0

を期待する(再び)ジョン、ありがとうございました。これは完璧です。私は、このプロジェクトのためにXMLで抱えていた問題を信じることはできません。 – CarlGanz

+0

@CarlGanz喜んで助けてください。私もXMLの遅れたユーザーでした。私はsnugoを見る、彼はXMLの忍者です –

関連する問題