Metode pintasan mungkin tidak cocok untuk ini. AUTO dan PATH tidak menyukai banyak elemen dengan nama yang sama. Sepertinya Anda harus menggunakan UNTUK XML EXPLICIT perintah.
Ini berhasil, tetapi rumit.
Contoh:
--Generate Sample Data
--FOR XML EXPLICIT requires two meta fields: Tag and Parent
--Tag is the ID of the current element.
--Parent is the ID of the parent element, or NULL for root element.
DECLARE @DataTable as table
(Tag int NOT NULL
, Parent int
, TaxonomyValue nvarchar(max)
, CategoryValue nvarchar(max)
, CategoryLevel int)
--Fill with sample data: Category Element (2), under Taxonomy(1), with no Taxonomy value.
INSERT INTO @DataTable
VALUES (2, 1, NULL, 1, 'Clothing')
, (2, 1, NULL, 2, 'Jeans')
--First part of query: Define the XML structure
SELECT
1 as Tag --root element
, NULL as Parent
, NULL as [Taxonomy!1] --Assign Taxonomy Element to the first element, aka root.
, NULL as [Category!2] --Assign Category Element as a child to Taxonomy.
, NULL as [Category!2!Level] --Give Category an Attribute 'Level'
--The actual data to fill the XML
UNION
SELECT
Data.Tag
, Data.Parent
, Data.TaxonomyValue
, Data.CategoryValue
, Data.CategoryLevel
FROM
@DataTable as Data
FOR XML EXPLICIT
Menghasilkan XML
<Taxonomy>
<Category Level="1">Clothing</Category>
<Category Level="2">Jeans</Category>
</Taxonomy>
Sunting:Apakah kolom dibalik. Tidak ada lagi level Jeans.