We help IT Professionals succeed at work.
Get Started

Controlling XML format with FOR XML PATH

1,186 Views
Last Modified: 2013-11-10
I have a project with a looming deadline that requires me to extract data from a number of related SQL tables into an XML file. I need precise control over the format of the XML file to meet the project's requirements and I think the best way will be to use the FOR XML PATH tag. As this is my first time I would greatly appreciate some expert assistance to kick start the process.

My example below is a greatly simplified form of a project to extract insurance document records from SQL Server 2008 R2 to XML. There are many more tables and columns to extract in the live project but if I can get my head around this fragment I should be able to build rapidly on this knowledge.

Firstly, I have three related tables as below. The relationship between Policies and Drivers is one-to-many and the relationship between Drivers and Medical_Conditions is one-to-many. Every policy must have at least one driver but each driver may have 0, 1 or more medical conditions.

Policies
======
policy_id (PK)
driving_restriction

Drivers
======
driver_id (PK)
policy_id (FK)
title
first_name
surname
license_type
years_held

Medical_Conditions
================
condition_id (PK)
driver_id (FK)
type_of_condition
first_diagnosed
dvla_advised

I need my XML to look like this:

<?xml version="1.0" encoding="utf-8"?>
<Insurance_Doc>
  <Driving_Restriction restriction_type="Named Drivers Only">
    <Driver>
      <title>Mr</title>
      <first_name>Fred</first_name>
      <surname>Bloggs</surname>
      <license_type>Full UK</license_type>
      <years_held>10+</years_held>
    </Driver>
  </Driving_Restriction>
  <Medical_Condition>
    <Driver>
      <title>Mr</title>
      <first_name>Fred</first_name>
      <surname>Bloggs</surname>
      <Condition_Detail>
        <type_of_condition>Diabetes Type 1</type_of_condition>
        <first_diagnosed>01/05/2001</first_diagnosed>
        <dvla_advised>Yes</dvla_advised>
      </Condition_Detail>
      <Condition_Detail>
        <type_of_condition>Heart Condition</type_of_condition>
        <first_diagnosed>26/05/2005</first_diagnosed>
        <dvla_advised>Yes</dvla_advised>
      </Condition_Detail>
    </Driver>
  </Medical_Condition>
</Insurance_Doc>

As you can see, the XML file is element centric but does also use attributes. Can some expert please provide the SQL SELECT statement that will result in the above XML output?

Many thanks in advance.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE