Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 FOR XML PATH question

Author  Topic 

jmillay
Starting Member

7 Posts

Posted - 2009-10-11 : 19:46:18
Hi--

The following query
SELECT '' AS "DATETIME/YEAR"
,MONTH(GETDATE()) AS "DATETIME/MONTH"
,DAY(GETDATE()) AS "DATETIME/DAY"
,Datepart(HH, GETDATE()) AS "DATETIME/HOUR"
FOR XML PATH('CNTROLAREA'), TYPE
results in:

<CNTROLAREA>
<DATETIME>
<YEAR></YEAR>
<MONTH>10</MONTH>
<DAY>11</DAY>
<HOUR>19</HOUR>
</DATETIME>
</CNTROLAREA>


You can see that the "YEAR" tag is "<YEAR></YEAR>"

When i run this query:
SELECT '',
(SELECT '' AS "DATETIME/YEAR"
,MONTH(GETDATE()) AS "DATETIME/MONTH"
,DAY(GETDATE()) AS "DATETIME/DAY"
,Datepart(HH, GETDATE()) AS "DATETIME/HOUR"
FOR XML PATH('CNTROLAREA'), TYPE
)
FOR XML PATH('PROCESS_PO_007')

I get:

<PROCESS_PO_007>
<CNTROLAREA>
<DATETIME>
<YEAR />
<MONTH>10</MONTH>
<DAY>11</DAY>
<HOUR>19</HOUR>
</DATETIME>
</CNTROLAREA>
</PROCESS_PO_007>

Why is "YEAR" now "<YEAR />" ? I've looked through documentation, tried to understand. I realize I can probably do some "jerry-rigging" to get it in the format I want.

My question is--does anyone know why the format of an "empty" tag would change when it is not part of the root element? and how to control it so it is part of the root element?

jmillay
Starting Member

7 Posts

Posted - 2009-10-11 : 19:50:03
Sorry, my last question "how to control it so it is part of the root element?" isn't what i meant to ask... i meant to ask "how to control it so it is "<YEAR></YEAR>" when empty?"

Thanks
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2009-10-11 : 20:55:35
I was thinking, with a script from Martin Honnen.


declare @xml xml
set @xml = (select NULL as 'year' for xml path (''), type, elements xsinil)



select @xml.query('if (/year/text()) then <YEAR></YEAR>')

select @xml


This is a sample , i'm not very good at sql so....
Go to Top of Page

sapator
Constraint Violating Yak Guru

462 Posts

Posted - 2009-10-11 : 20:56:58
Also you can see that also the other empty elements return </element>
Go to Top of Page

jmillay
Starting Member

7 Posts

Posted - 2009-10-11 : 23:47:47
I appreciate the response. I don't understand what you mean, though. Is there someway i can get "<YEAR></YEAR>" without doing an if statement? Why does the XML data type return it as "<YEAR></YEAR>" when it is in the root element?
Go to Top of Page

jmillay
Starting Member

7 Posts

Posted - 2009-10-12 : 00:02:52
I found this link:

http://social.msdn.microsoft.com/Forums/en/sqlxml/thread/c2a1732c-7f0f-4dba-91b5-e0d66240a702

which asks a very similar question to mine. the reason i need it to be "<tag></tag>" is becuase the business partner wants it to be that way. I've always thought there was no difference between "<tag></tag>" and "<tag />", that any xml parser would just see the difference. And, that's what the topic in the above link is saying. I'm going to go back to my business partner and have them get a real XML parsing process.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-10-12 : 01:45:47
It seems your partner doesn't handle the XML correctly.
Maybe your partner has written a string parser of their own?


N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page
   

- Advertisement -