473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FOR XML PATH and dynamic attribute names

Hey,

I have a question regarding XML FOR PATH and SQL Server 2005. I have been using it successfully but I have run in to an issue I was hoping you could help with.

I have a couple of tables and I want to do this:

Expand|Select|Wrap|Line Numbers
  1. select              
  2.         StringValue as '@' + XmlFieldName, 
  3. from defaultaddressfielddefs daf
  4. inner join addressfields af on af.AddressFieldDefId = daf.AddressFieldDefId
  5. for xml path('Address'), type
  6.  
so, I KNOW this will cause an SQL error, I am just trying to show you the concept. Basically, I want to be able to change the attribute name from "StringValue" to a field in my row called XmlFieldName and have that attribute set to the value that is contained in the StringValue column.

How can this be done within a select statement?

Thanks!
Brian
Apr 5 '07 #1
5 6427
iburyak
1,017 Expert 512MB
Try this:


[PHP]select 1 TAG,
null Parent,
XmlFieldName [Default!1!FileName]
from defaultaddressfielddefs daf
inner join addressfields af on af.AddressFieldDefId = daf.AddressFieldDefId
FOR XML EXPLICIT[/PHP]

Good Luck.
Apr 5 '07 #2
I am not sure how that would work. That syntax requires me to specify the attribute name in the [Default!1!FileName] syntax. I need the place where you have 'FileName' to have a dynamic name based on what is in the XmlFieldName column.


Try this:


[PHP]select 1 TAG,
null Parent,
XmlFieldName [Default!1!FileName]
from defaultaddressfielddefs daf
inner join addressfields af on af.AddressFieldDefId = daf.AddressFieldDefId
FOR XML EXPLICIT[/PHP]

Good Luck.
Apr 5 '07 #3
iburyak
1,017 Expert 512MB
Did you try this examle?

In line below

[PHP]XmlFieldName [Default!1!FileName]

XmlFieldName - is a column name from your table.
Default - can be changed it is a tag name I made up.
FileName - can be changed it is an atribute name I made up.[/PHP]
Apr 5 '07 #4
I am fairly familiar with the XML EXPLICIT syntax. Unless we are on a different page completely, I don't know how this will work for what I need. Here is an example of what I want to do.

Here is my TABLE

ID XmlFieldName StringValue
1 Pepsi Free
2 Coke Zero
...

I want to make the resulting XML be, for every row

<ROOT>
<Default Pepsi="Free">
<Default Coke="Zero">
</ROOT>

It isn't a matter of changing the attribute name once, as in your example and using that for every attribute. It is a matter of changing the attribute to be the actual value in the XmlFieldName column and making that attribute value be "SomeString" (the value in my StringValue column)

Thanks for your help, I hope there is a solution to this.


Did you try this examle?

In line below

[PHP]XmlFieldName [Default!1!FileName]

XmlFieldName - is a column name from your table.
Default - can be changed it is a tag name I made up.
FileName - can be changed it is an atribute name I made up.[/PHP]
Apr 5 '07 #5
iburyak
1,017 Expert 512MB
I never saw XML not talking of a query where attribute is a variable.
Well formed XML has static attributes.

The only way I know how to do is

[PHP]<ROOT>
<Default name="Pepsi" value="Free">
<Default name="Coke" vlue="Zero">
</ROOT>[/PHP]


Let me know if you are interested.
Thank you
Apr 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: mic | last post by:
I've spent last hour trying to debug my code, when I found out that instead of executing object's method I accidentaly overridden it with variable (see below example). My stupid! But as the system...
0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
2
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." +...
4
by: pizzy | last post by:
INTRO: I tried to clean it up for easy reading. I hope I didn't make any mistakes. PROBLEM: WOW, this is some crazy sh!t. I can't get my checkbox (see "TAGSELECTED") to print my textboxes (see...
7
by: mittal.pradeep | last post by:
What is the better table design for a data collection application. 1. Vertical model (pk, attributeName, AttributeValue) 2. Custom columns (pk, custom1, custom2, custom3...custom50) Since the...
3
by: Dave Rose | last post by:
Hello all. I was wondering if creating classes could be dynamic. I want to know if I can make a class Person, then read in a list of names (say people's names) so then I can have a class instance...
22
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I...
5
by: 7stud | last post by:
Here's the code: ------------ import os, os.path, pprint mydir = "/Users/me/2testing" files = pprint.pprint(files) print os.path.join(mydir, "helloWorld.py")
2
by: mhuff | last post by:
Hello, I have a table called ticket that has tickets stored in it. I have a seperate table called line items with the line items in it that are tied back to the ticket via a foreignkey. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.