SQLTeam.com | Weblogs | Forums

XML File Namespace Query

Hi everyone,

I have generated a XML file of a database in SQL which is printing some not wanted data.
How can I skip this data being generated with the file?
I just want a simple file with normal nametags and if null value is there then just print that tag....
Please see screenshot:

Can you provide DDL, sample data and the query you used to generate the script above. Also, if the value is null, do you want to not show the attribute?

What is thw purpose of this xml file? What uses it

Yes I want to show the attribute.
How do I provide you the data? The database is too big.
This is the query I used:

     -- ,
     -- ,[List_Provider]
    --  ,[Issuing_Body]
			--,[Catefor xml path ry]
			--,[Sub_Catefor xml path ry]

				SELECT TOP 100
				 -- ,[NatId_Record_ID_FK]
				  from National_Identity
				  --where NatId_Record_ID_FK=Record_ID
				  where NatId_UniqueID_FK=UniqueID
				  for xml path('National_Identity'), root('National_Identitities') , elements xsinil, type) as [*],

					SELECT TOP 100
					  From Address
					where Address_UniqueID_FK = UniqueID
					--where Address_Record_ID_FK = Record_ID
					for xml path('Address'), root('Addresses'), elements xsinil,type) as [*],

							SELECT TOP 100
							 -- ,[Alias_Record_ID_FK]
							 -- ,[Alias_UniqueID_FK]
								from Alias
								 where Alias_UniqueID_FK = UniqueID
								 --where Alias_Record_ID_FK = Record_ID
								 for xml path('Alias'), root('Aliases'), elements xsinil, type) as [*],

									SELECT TOP 100
									 -- ,
									  from Birth_Details
									  --where Birth_Details_Record_ID_FK = Record_ID
									 where Birth_Details_UniqueID_FK = UniqueID
									 for xml path('Birth_Detail'), root('Birth_Details'), elements xsinil, type) as [*],

									 SELECT TOP 100
									 -- ,[Death_Details_Record_ID_FK]
									 -- ,[Death_Details_UniqueID_FK]
									 -- ,
									  from Death_Details
									 --where Death_Details_Record_ID_FK = Record_ID
									 where Death_Details_UniqueID_FK = UniqueID
									 for xml path('Death_Detail'), root('Death_Details'), elements xsinil, type) as [*],

									 SELECT TOP 100
									  from Contact_Info
									 --where ConInfo_Record_ID_FK = Record_ID
									 where ConInfo_UniqueID_FK = UniqueID
									 for xml path('Contact_Info'), root('Contact_Infos'), elements xsinil, type) as [*],

									 SELECT TOP 100
								  from Individual_Association
								 --where IndAssoc_Record_ID_FK = Record_ID
								 where IndAssoc_UniqueID_FK = UniqueID
								  for xml path('Individual_Association'), root('Individual_Associations'), elements xsinil, type) as [*],

								  SELECT TOP 100
								 -- ,
								 -- ,[BusAssoc_Record_ID_FK]
								 -- ,[BusAssoc_UniqueID_FK]
								 from Business_Association
									 --where BusAssoc_Record_ID_FK= Record_ID
									 where BusAssoc_UniqueID_FK= UniqueID
									  for xml path('Business_Association'), root('Business_Associations'), elements xsinil, type) as [*],

									  SELECT TOP 100
									 -- ,
									 from Article
									 --where Article_Record_ID_FK= Record_ID
									 where Article_UniqueID_FK= UniqueID
									  for xml path('Article'), root('Articles'), elements xsinil, type) as [*],

									  SELECT TOP 100
									 -- ,[Note_Record_ID_FK]
									 -- ,[Note_UniqueID_FK]
									   from Note
										 --where Note_Record_ID_FK= Record_ID
										  where Note_UniqueID_FK= UniqueID
										  for xml path('Note'), root('Notes'), elements xsinil, type) as [*],

										  SELECT TOP 100
										--  ,
										  from Political_Position
										 --where PolPos_Record_ID_FK= Record_ID
										 where PolPos_UniqueID_FK= UniqueID
										  for xml path('Political_Position'), root('Political_Positions'), elements xsinil, type) as [*],

										  SELECT TOP 100
										  from Publishing_Details
										 --where PubDet_Record_ID_FK= Record_ID
										 where PubDet_UniqueID_FK= UniqueID
										  for xml path('Publishing_Detail'), root('Publishing_Details'), elements xsinil, type) as [*],

										SELECT TOP 100
									--  ,[PIden_ID]
									  from Photo_Identity
									 --where PIden_Record_ID_FK= Record_ID
									 where PIden_UniqueID_FK= UniqueID
									  for xml path('Photo_Identity'), root('Photo_Identities'), elements xsinil, type) as [*]

from Individual
WHERE  [Individual_List_ID_FK] = [List_ID]
for xml path ('Individual'), root('Individuals'), elements xsinil, type) as [*]

from [dbo].[List]
--where List_ID = [Individual_List_ID_FK]
  for xml path('List'), root ('Lists'), elements xsinil, type) as [final]

Why does it matter?

As @GHW901 said, it does not matter. What you are showing is perfectly valid XML. When you construct an XML from values in a table, and if a value is null, you can either choose to omit that element, or show it as null using xsi:nil construct as you have in your example.

If I understand your wording, if a value is null, what you want is to show is, something like "<Address_Line_1/>" or "<Address_Line_1><Address_Line_1>". But this does not represent a null. It represents an empty string.

You can see this from the following example:

CREATE TABLE #tmp(Col1 VARCHAR(32), Col2 VARCHAR(32));

INSERT INTO #tmp VALUES ('x1','y1'),('x2',NULL),('x3','');


This produces the following output.

<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Col2 xsi:nil="true" />

Notice the difference col2 is represented when the value is null vs when it is an empty string.

If you don't want the null elements at all (i.e., the xsi:nil="true"), then omit the "ELEMENTS XSINIL" from the query.

If you want to represent null values as empty strings, use COALESCE. That is, in this example, instead of "SELECT *", you would do "SELECT COALESCE(Col1,''),COALESCE(Col2,'')".

1 Like

Ok I will check this and confirm whether its working or not.
Also what about the information coming in main tags?
like for example:

        <Record_Type xsi:nil="true" />
        <Title xsi:nil="true" />
        <Alternate_Title xsi:nil="true" />
        <National_Identitities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Addresses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <Address_Line_1 xsi:nil="true" />
            <Address_Line_2 `Preformatted text`xsi:nil="true" />
            <Address_Line_3 xsi:nil="true" />

I dont want this information to print: <National_Identitities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
but just simply <National_Identities>
and other place this link is printing...

it matters because many of us have worked with xml for many years. so by you sharing what it is used for we might be able to provide you guidance with maybe even avoiding the use of xml as it can get pretty complicated and brittle. If you are interested that is. or we can just provide you on how to generate the xml

1 Like

you need to provide DDL and DML for all of these tables

meaning in line sql script

create table #National_Identity(NatID_Type varchar(50),NatID_Num int, ) --etc

insert into #National_Identity
select 'VET', 3 --etc

This helps us emulate your data (which we do not have access to) in our environment.

1 Like

Ah I see. Thank you for explaining it so well. Basically I have been given this task from someone and they asked to complete it like this. So I have to convert my SQL Tables data into XML so it can be used somewhere else.

Can you please tell me whats the best way to copy all the queries and send it here? I want to learn the procedure.

I checked using COALESCE but its not working. Its actually printing the data horizontally now and incomplete.

Give the columns names as in

    COALESCE(Col1,'') as Col1,
    COALESCE(Col2,'') as Col2

The reference to "http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that will be reading your xml document which schema instance namespace is to be used for interpreting the special attributes (in this case xsi:nil). If you omit it most parsers will still parse the document correctly, but to be precise you need to include it.

1 Like

Already did here ^^

DROP TABLE IF EXISTS #National_Identity;
CREATE TABLE #National_Identity(
NatId_UniqueID_FK int,
[NatID_Type] varchar(50)
,[NatID_Num] int
,[NatID_issue] varchar(50)
,[NatID_expiry] datetime
,[NatID_State_Province] CHAR(2)
,[NatID_Country] varchar(50)
,[isNationality] bit

INSERT INTO #National_Identity(NatId_UniqueID_FK,[NatID_Type]
VALUES (1, 'XDD',1,'None',getdate(),'ON','Canada',1);

Address_UniqueID_FK int,
[Address_Line_1] varchar(50)
,[Address_Line_2] varchar(50)
,[Address_Line_3] varchar(50)
,[Address_Line_4] varchar(50)
,[Town_District] varchar(50)
,[City] varchar(50)
,[County_State] varchar(50)
,[Post_Zip_Code] varchar(50)
,[Country] varchar(50)
,[ISO_Country] varchar(50)
,[Soft_Delete_Address] bit

INSERT INTO #Address(Address_UniqueID_FK,
VALUES (1, '12345 Kiev Street',null, null,null,'Donbas','Kyiv','Donbas','1234-99','Ukraine','UA',1),
(1, null, '111 Dobvrisky Street',null,null,'LVIV','Khershon','Donbas','1234-99','Ukraine','UA',1);

   FROM #National_Identity ni
   join #Address a on ni.NatId_UniqueID_FK = a.Address_UniqueID_FK
    FOR XML PATH('addresses'),ROOT('Document')
1 Like

Thank you. It works. I was missing "as" keyword.
I understand about the link but my requirement is to remove it. Even if I face the problem later I will change it back but it would be really helpful if I remove it right now.

But this is not printing the tags will null values.
My requirement is that if there is a tag will NULL then it should just simply print that tag like this:

<Title />
    <Alternate_Title />

This one is resolved by @JamesK

Only thing is remaining is the link:

  <Individuals xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

I just want to hide it from the final output.
so my XML file would look like this:

yes I am also searching for the same answer like the equation for null