[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

Nested FOR XML in SQL Server 2005

I'm struggling to use FOR XML to create a desired layout of XML.
The problem comes in creating sibling elements with the same element names.

...I need one of each elements <Brand> and <ItemType> derived from distinct rows from one table.
I also need multiple sibling elements <BulletPoint> derived from all rows of another table.

- The file "right.xml" shows what I want.

- The attached code snippet is SQL code which will generate temp tables and do what I currently do.

- The file "wrong.xml" is the XML that I currently produce (with the "sibling" items appearing in the wrong place).

How can I get the desired result?

NOTE - the example given has been simplified down from the actual one.

  BulletPoint varchar(500)
INSERT INTO @BulletPoints SELECT '12 month guarantee'
INSERT INTO @BulletPoints SELECT 'Works straight out of the box (just fit the supplied batteries)'
INSERT INTO @BulletPoints SELECT 'No programming or setup required'

  ID int,
  ean varchar(13),
  partcode varchar(50)
INSERT INTO @Stock VALUES (1, '1231231231231', 'PART_001')
INSERT INTO @Stock VALUES (2, '1231231231232', 'PART_002')
INSERT INTO @Stock VALUES (3, '1231231231233', 'PART_003')

	'1.01' AS [Header/DocumentVersion],
	'Product' AS [MessageType],
	ID As "MessageID",
	partcode AS "Product/SKU",
	'EAN' As "Product/StandardProductID/Type",
	ean As "Product/StandardProductID/Value",
	'Sony' As "Product/DescriptionData/Brand",

	(SELECT BulletPoint As "BulletPoint" FROM @BulletPoints FOR XML PATH(''), type ),

	'ConsumerElectronics' As "Product/DescriptionData/ItemType"
	FROM @Stock FOR XML PATH('Message'), type)
FOR XML PATH('AmazonEnvelope'), type

Open in new window

  • 3
1 Solution
BeamsonAuthor Commented:
I'm afraid not.

The creating XML article shows how you can create DIFFERENT <color1>, <color2> etc elements in one common container element <colors>.

I need to create multiple elements with THE SAME element name at the same level in the heirarchy as other elements.

The reading XML article is not relevant to this problem (I'm trying to write it, not read it ! )
BeamsonAuthor Commented:
Solved it!

The central nested FOR XML needs to have the final element name (BulletPoint) in the As part of the SELECT statement and the higher element parent names (Product/DescriptionData) in the AS for the whole statement.
Substituting the line below into the middle of my code solves the problem:

(SELECT BulletPoint As "BulletPoint" FROM @BulletSection FOR XML PATH(''), type ) As "Product/DescriptionData",

BeamsonAuthor Commented:
This solves the problem.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now