 |
|
 |
|
Next: update query problem
|
| Author |
Message |
External

Since: Oct 23, 2008 Posts: 2
|
(Msg. 1) Posted: Thu Oct 23, 2008 2:33 am
Post subject: FOR XML PATH adding some extra data not in the database Archived from groups: microsoft>public>sqlserver>xml (more info?)
|
|
|
Bit of a weird question (possibly)
I have a sql server database that has amongst other things data
locations (lat,longs) stored in degrees and heights stored in metres
The XSD I have stipulates that the format like this (in XML)
<Location>
<Latitude Units="Degrees">37.0</Latitude>
<Longitude Units="Degrees">-80.0</Longitude>
<Height Units = "m">100</Height>
</Location>
the lat/long units can also be "Radians" and the height can be in km
(but that is not too important for my question).
Ignoring the "Units" attribute for now I can use FOR SQL PATH as such
select dl.Latitude as "Latitude", dl.Longitude as "Longitude",
dl.Height as "Height" from Location dl where dl.DeploymentID = 1 for
xml path('Location'),Type
But if I want to add the attributes (given that the units data is not
in my DB) I can use
select 'Degrees' as "Latitude/@Units", dl.Latitude as
"Latitude",'Degrees' as "Longitude/@Units", dl.Longitude as
"Longitude", 'm' as "Height/@Units", dl.Height as "Height" from
Location dl where dl.DeploymentID = 1 order by EntryID for xml
path('Location'),Type
<Location>
<Latitude Units="Degrees">8.520121449583158e-001</Latitude>
<Longitude Units="Degrees">-6.050532917888742e-002</Longitude>
<Height Units="m">124</Height>
</Location>
All looks fine so far.
But in some cases the height data in the database is null (where that
height is not known/measured) and obviously I want to handle this.
The problem is using the above SQL I get the following for NULL
heights
<Location>
<Latitude Units="Degrees">8.520121449583158e-001</Latitude>
<Longitude Units="Degrees">-6.050532917888742e-002</Longitude>
<Height Units="m"/>
</Location>
What I would ideally want is for the Height not to appear at all in
this case. But for the life of me I cant work out how to do it!
Can anyone offer any help or indeed advice on whether outputting the
Height element like this is considered bad xml practice (I am new to
all this!)
Note: the Height element in marked as minOccurs="0" in the XSD
Many, many thanks for your time. >> Stay informed about: FOR XML PATH adding some extra data not in the database |
|
| Back to top |
|
 |  |
External

Since: Feb 08, 2005 Posts: 182
|
(Msg. 2) Posted: Thu Oct 23, 2008 5:41 am
Post subject: RE: FOR XML PATH adding some extra data not in the database [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Try this:
SELECT
'Degrees' as "Latitude/@Units",
dl.Latitude as "Latitude",
'Degrees' as "Longitude/@Units",
dl.Longitude as "Longitude",
CASE WHEN dl.Height IS NOT NULL THEN 'm' END AS "Height/@Units",
dl.Height as "Height"
FROM Location dl
WHERE dl.DeploymentID = 1
ORDER BY EntryID
FOR XML PATH ( 'Location' ), TYPE
"ajtaylor@hushmail.com" wrote:
> Bit of a weird question (possibly)
>
> I have a sql server database that has amongst other things data
> locations (lat,longs) stored in degrees and heights stored in metres
>
> The XSD I have stipulates that the format like this (in XML)
>
> <Location>
> <Latitude Units="Degrees">37.0</Latitude>
> <Longitude Units="Degrees">-80.0</Longitude>
> <Height Units = "m">100</Height>
> </Location>
>
> the lat/long units can also be "Radians" and the height can be in km
> (but that is not too important for my question).
>
> Ignoring the "Units" attribute for now I can use FOR SQL PATH as such
>
> select dl.Latitude as "Latitude", dl.Longitude as "Longitude",
> dl.Height as "Height" from Location dl where dl.DeploymentID = 1 for
> xml path('Location'),Type
>
> But if I want to add the attributes (given that the units data is not
> in my DB) I can use
>
> select 'Degrees' as "Latitude/@Units", dl.Latitude as
> "Latitude",'Degrees' as "Longitude/@Units", dl.Longitude as
> "Longitude", 'm' as "Height/@Units", dl.Height as "Height" from
> Location dl where dl.DeploymentID = 1 order by EntryID for xml
> path('Location'),Type
>
> <Location>
> <Latitude Units="Degrees">8.520121449583158e-001</Latitude>
> <Longitude Units="Degrees">-6.050532917888742e-002</Longitude>
> <Height Units="m">124</Height>
> </Location>
>
> All looks fine so far.
>
> But in some cases the height data in the database is null (where that
> height is not known/measured) and obviously I want to handle this.
>
> The problem is using the above SQL I get the following for NULL
> heights
>
> <Location>
> <Latitude Units="Degrees">8.520121449583158e-001</Latitude>
> <Longitude Units="Degrees">-6.050532917888742e-002</Longitude>
> <Height Units="m"/>
> </Location>
>
> What I would ideally want is for the Height not to appear at all in
> this case. But for the life of me I cant work out how to do it!
>
> Can anyone offer any help or indeed advice on whether outputting the
> Height element like this is considered bad xml practice (I am new to
> all this!)
>
> Note: the Height element in marked as minOccurs="0" in the XSD
>
> Many, many thanks for your time.
>
>
>
>
> >> Stay informed about: FOR XML PATH adding some extra data not in the database |
|
| Back to top |
|
 |  |
External

Since: Oct 23, 2008 Posts: 2
|
(Msg. 3) Posted: Thu Oct 23, 2008 8:52 am
Post subject: Re: FOR XML PATH adding some extra data not in the database [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
On 23 Oct, 13:41, Bob wrote:
> Try this:
>
> SELECT
> 'Degrees' as "Latitude/@Units",
> dl.Latitude as "Latitude",
> 'Degrees' as "Longitude/@Units",
> dl.Longitude as "Longitude",
> CASE WHEN dl.Height IS NOT NULL THEN 'm' END AS "Height/@Units",
> dl.Height as "Height"
> FROM Location dl
> WHERE dl.DeploymentID = 1
> ORDER BY EntryID
> FOR XML PATH ( 'Location' ), TYPE
>
Bob,
You are a star!
Many thanks. >> Stay informed about: FOR XML PATH adding some extra data not in the database |
|
| Back to top |
|
 |  |
External

Since: Feb 08, 2005 Posts: 182
|
(Msg. 4) Posted: Tue Nov 04, 2008 4:10 am
Post subject: Re: FOR XML PATH adding some extra data not in the database [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Rate the post please.
"ajtaylor@hushmail.com" wrote:
> On 23 Oct, 13:41, Bob wrote:
> > Try this:
> >
> > SELECT
> > 'Degrees' as "Latitude/@Units",
> > dl.Latitude as "Latitude",
> > 'Degrees' as "Longitude/@Units",
> > dl.Longitude as "Longitude",
> > CASE WHEN dl.Height IS NOT NULL THEN 'm' END AS "Height/@Units",
> > dl.Height as "Height"
> > FROM Location dl
> > WHERE dl.DeploymentID = 1
> > ORDER BY EntryID
> > FOR XML PATH ( 'Location' ), TYPE
> >
>
> Bob,
>
> You are a star!
>
> Many thanks.
>
>
>
>
> >> Stay informed about: FOR XML PATH adding some extra data not in the database |
|
| Back to top |
|
 |  |
| Related Topics: | How to add some attributes when I use For Xml Path? - How to add some attributes when I use For Xml Path? As I developed the sql statement using For Xml Path, the users now required add new attributes to some tags. For Example: SQL: Select TOP 2 Code, Name From City For Xml Path('City') <City> ...
SQL 2005 For Xml Path question - In the SQL 2005 Books Online section titled "Using PATH Mode", there is an example near the bottom that results in the following XML. Is there a way to get the xml so it's encoded correctly (not using < >)? Not sure why it's doing th...
tuning question, relation between indexes and "for xml pat.. - ,Hi, I'm facing very strange problem here. I made index analysis by quering dynamic views, and found that bunch of indexes is not being used at all. Beside that, I found bunch of indexes which are duplicated. For example, on the same set of columns,..
Loading XML Schema from a file using a RELATIVE path - Good Evening All, I've seen examples of how to load an XML schema from a file; however, all of them use an absolute file path. I'd like to load it from a path relative to the location were the SQL script is. This will allow us to access the XSD file..
XML Output Adding Line Breaks - Hello, I am running SQL Server 2000 and am attempting to query data and save the results to an XML document. The problem is that for some reason, it is adding line breaks in the middle of the data making the XML doc useless. I have tried all sorts of.. |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|