我在SQL Server中有以下XML,並且想要遍歷它以生成結構化消息。SQL Server XQuery - 迭代動態xml並返回自定義結構
<aggregate type="ApplicationForm">
<entity type="Form" root="true" id="d799728b-7973-4046-b60f-cb25d4ee385c">
<attribute name="creationDate" multivalue="false">2017-01-16</attribute>
<attribute name="product" multivalue="false">Abc</attribute>
<relation name="r_PersonMain" multivalue="false">4808f654-f480-412c-8dc5-d29c6c811602</relation>
<relation name="r_PersonPayer" multivalue="false">a8e9eaf2-56a5-4f88-955b-19eb98f6e882</relation>
</entity>
<entity type="Payment" root="true" id="e197bf66-1e35-42a9-bdc0-0674e3a0f765">
<attribute name="totalAnnualPremium" multivalue="false">328415.81</attribute>
<relation name="r_PaymentMethodRecurring" multivalue="false">b8b3c652-b886-44aa-a75b-b2d3ecd6c064</relation>
<relation name="r_PaymentMethodFirst" multivalue="false">f3d91f99-ff6a-4888-a663-24e42ecc7342</relation>
<attribute name="term" multivalue="false">01</attribute>
</entity>
<entity type="Person" root="false" id="4808f654-f480-412c-8dc5-d29c6c811602">
<relation name="r_AddressWork" multivalue="false">cae83657-47c2-49bd-a588-7685271c4766</relation>
<attribute name="idNumber" multivalue="false">1112223334447</attribute>
<relation name="r_SelectionItem" multivalue="true">...</relation>
<relation name="r_Health" multivalue="false">07d08bd6-ec73-4710-9de4-23435cd2b088</relation>
<relation name="r_AddressCurrent" multivalue="false">56d17bda-e332-497e-8e22-e7b7f09f996d</relation>
<attribute name="lastName" multivalue="false"> 1</attribute>
<attribute name="jobDescription1" multivalue="false"/>
<relation name="r_Behavior" multivalue="false">2db2c23a-37dd-4857-87b4-005aa87b2c2d</relation>
<attribute name="email" multivalue="false"></attribute>
<relation name="r_AddressRegistered" multivalue="false">ce79a468-fb26-4996-91a8-82954d960855</relation>
<attribute name="telephoneExtention1" multivalue="false"/>
<relation name="r_Occupation1" multivalue="false">b7b69acc-2945-4f64-8ffd-4537849280f5</relation>
</entity>
</aggregate>
頂部元素包含長列表entity
標籤。它每個都包含兩個元素,attribute
& relation
。 attribute
包含直接值,而relation
包含對另一個entity
標籤的引用,該標籤又包含attribute
或relation
。
爲了挑選出entity
表格(第一個標籤),它需要迭代它及其所有引用,直到檢索到所有引用的entities
。
我可以檢索第一個實體及其關係(參考實體),然後訪問它的第二級參考實體,然後再次檢查它的內部是否存在另一個「關係」標記,那麼我也必須訪問它。
問題是,這種方法不是動態的,不能自動檢索所有引用的項目。問:我如何動態地訪問所有引用的實體及其屬性,直到沒有引用的實體了。問:我還想根據每個屬性或實體「名稱」標籤賦予我的自定義名稱分配標籤。例如creationDate,product。
這是我的查詢。
DECLARE @xml xml
SET @xml =(Select CAST(CAST([AAHAD].[dbo].[aq_aggregate].data AS NVARCHAR(MAX)) AS XML)
FROM [AAHAD].[dbo].[aq_aggregate]
WHERE [AAHAD].[dbo].[aq_aggregate].[aggregateId] = 2
FOR XML RAW, TYPE)
SELECT @xml.query('
let $xml := (/row/aggregate)
let $form := (/row/aggregate/entity[@type="Form"])
return
<Form>
<attributes>
{
for $form_attrs in ($form/attribute)
return <attribute><name>{ data($form_attrs/@name) } </name><value>{ data($form_attrs) }</value></attribute>
}
</attributes>
<relations>
{
for $form_rel in ($form/relation)
let $form_rel_id := data($form_rel)
let $relation :=($xml/entity[@id=$form_rel_id])
return
<relation>
<attributes>
{
for $innerRel_attrs in ($relation/attribute)
return <attribute><name>{ data($innerRel_attrs/@name) } </name><value>{ data($innerRel_attrs) }</value></attribute>
}
</attributes>
<relations>
{
for $innerRel_rel in ($relation/relation)
let $inner_Rel_id := data($innerRel_rel)
let $inner_Relation :=($xml/entity[@id=$inner_Rel_id])
return
<relation>
<attributes>
{
for $inner2Rel_attrs in ($inner_Relation/attribute)
return <attribute><name>{ data($inner2Rel_attrs/@name) } </name><value>{ data($inner2Rel_attrs) }</value></attribute>
}
</attributes>
<relations>
{
for $inner2Rel_rel in ($inner_Relation/relation)
return <relation>{ ($inner2Rel_rel) }</relation>
}
</relations>
</relation>
}
</relations>
</relation>
}
</relations>
</Form>
')
謝謝你的回答,雖然我在尋找類似的分層格式來生成,而不是表格。所以通過'ID'引用的所有元素都應該作爲父元素的子元素來使用。 – AAhad
@AAhad這就是爲什麼你應該總是發佈**期望的輸出**。我不知道(我應該怎麼辦?)你需要什麼......如果你不提供更多的細節(閱讀我的最後一行),這是不可能回答的。以下鏈接的實體將導致*遞歸CTE *。 – Shnugo
我正在尋找方法將相關實體置於一個父元素之下,那就是我一直在尋找的東西。 – AAhad