Declare Variable In Xml File
This section describes the 'variable' element, which is used in the content of a 'template' element. The 'variable' element is an XSLT statement that declares a.
Give More Feedback
Irfan Patel is a Bachelor of Computer Application He holds a MCSD, MCP.NET, ITIL Certifications and has worked extensively with C#, ASP.Net, SQL Server since 1998. He has also trained various programming languages and tools, RDBMS & OS platforms to more than 1000 students. His expertise comes from various industries viz Jewellery, Shipping,Automobiles, RFID projects, Ticket Reservation systems and e-commerce websites. Besides teaching and programming he loves music, singing, dancing, bikes & following his favourite football team Chelsea.
Smartboy786 5-Jun-12 4:14 5-Jun-12 4:14 I have got an issue.I am new to xml so need some help from u.I have to store the signup form information in local xml file. I have got a task which I am not able to complete.The task is: CreateSign up page task -1: sign up page validations( from javascript) ( email validation date validation ) (date time picker 3rd party control integration). First name Last name: username: emailid: dob: country: task -2: successfully sign up user store in local xml page. Task -3: list of users. Show in grid task -4: Edit page.
Ps help me brother. ChizI 13-Jan-10 5:54 13-Jan-10 5:54 Not mentioning what the other mentioned below, because that part is obvious, I like how you give an XML example then represent it with the code. Some have code examples without the XML that they are testing with.
However, why only write a portion of an article? Articles about a subject should cover all the major aspects of that subject? When talking about XML, you should talk about Reading from file to a string, Searching the string, Updating the string, and Saving from string to an XML file. Most people are not generating going around just creating XML files.
Beginners will create the XML file on their HD based on the demo, then use example code to read it, search through it for specific data, update it, then write it back to the XML file. This covers the most important aspects of XML and will help a beginner better understanding of how this information is used. Supermankelly 6-Aug-08 7:10 6-Aug-08 7:10 But this below step i can't follow. Im new to Visual C# 2008 which I am using. Where is XML menu? (XML Create Schema) Creating the schema (.XSD) file for validating our just made.xml file) Go to menu XML - Create Schema.

Notice that in the solution explorer, it has created a file XMLFile1.xsd. Also notice what the IDE (Integrated Development Environment) has added in tag of ours. It signifies our.xml file will be validated against which Schema file.
It often becomes necessary to create, shred, combine, or otherwise reconstitute XML data, in order to make it fit a specific purpose. Sometimes business requirements dictate that XML fragments should be merged, while other requests call for XML documents or fields to be shredded and their values imported into tables. Sometimes, XML Data must be created directly from existing tables.
SQL Server provides plenty of XML-related tools, but how can we know which ones to use, and when? Let’s examine some of these tasks that require XML manipulation, using the sample database (Other AdventureWorks versions should work OK, but there may be variations in the data and/or table schemas).
Creating XML One common requirement is to create an XML structure that is based on the schema of an existing table. Let’s assume that we’ve received a request to create XML data from relevant fields in the Person.Person table, for the person having BusinessEntityID 10 0 01. We need to gather the values from this row. GO We need to include the BusinessEntityID field and some of the name-data columns in the new XML structure. Note that there happens to be an existing XML column in the table – the Demographics field. SQL Server provides an XML option to use with the FOR clause, allowing for an easy method of converting table data into XML nodes. Can take different arguments – let’s find out which one works for us.
The argument is one of the simplest to use. It creates one node for each record returned by the SELECT clause. GO We see that an existing XML field is created as a nested node element. Note that the XML namespace data is included in the nested node. Shredding XML ‘Shredding’ XML data is another common request. To ‘shred’ means to strip the actual data away from the markup tags, and organize it into a relational format.
For example, shredding is what happens when an XML document is imported into a table, when each node value is mapped to a specific field in the table. A popular method to use for this is to use the function, but can also be engaged to perform the same tasks. OPENXML was available to use for shredding before the SQL Server XQuery methods were introduced, and is somewhat faster for larger data operations. However, it is decidedly more complex to use, and is more memory intensive.
Also, OPENXML cannot take advantage of as XQuery methods can. We’ve received another request: pull data from some of the nodes ( Occupation, Education, HomeOwnerFlag, NumberCarsOwned) that are contained in the Person.Person table’s Demographics XML column for BusinessEntityID 15291, and display it along with other non-XML field values ( FirstName, MiddleName, LastName) from the table.
The Person.Person record for BusinessEntityID 15291, and their expanded Demographics XML instance are shown below. GO XML Namespaces While this returns the shredded result that we want, the repetitive namespace declarations expand the size of our query – since we are returning four XML node values, we have to declare the namespace four times. Declaring the namespace is necessary because the Demographics XML structure uses – its XML data is associated with an XML schema. However, we can use a clause to declare the XML namespace instead – this lets us to declare the namespace only once for the entire code block. GO We’ve used the nodes method to drill down (one level) to the location of the ‘ IndividualSurvey‘ node, and then returned the actual values via the XQuery value method.
We used CROSS APPLY to join the node set back to the table. A CROSS APPLY would not have been necessary if we were shredding an XML variable, instead of from an XML column in a table. The nodes method easily shreds XML data from XML columns as well as from XML variables. The nodes method requires table and column aliases ( T (C )) in order for other XQuery methods (like the value method) to access the node set that the nodes method returns. The column and table alias names are irrelevant. Notice that we’ve used a clause to declare the XML namespace. Declaring the namespace is necessary because the Demographics XML structure uses – its XML data is associated with an XML schema.
Now that our XML data has been shredded, the results can be stored in a table or combined with other queries. Nodes Method Application and Efficiency Keep in mind that using the nodes method in simple queries can unnecessarily reduce query efficiency. When run in the same batch, our query that used the nodes method cost 54% of the batch, where the value-method-only query cost just 46%. GO In light of this, why use the nodes method at all? For simple queries, it’s probably better not to use it, although the batch cost or query time differences may be negligible.
See More On Stackoverflow
We’ve shown the use of nodes in a very basic example, but it can also be used in more complex queries where it is necessary to return – using nodes on a nodes result. It’s also very convenient to use for constructing new XML from existing nodes. Since nodes works by rendering logical portions of XML instances as node sets, it’s ideal for when query results must be returned in node form. Combining XML Another, perhaps more uncommon procedure, would be to merge XML data from different instances. Let’s do just that to demonstrate – we’ll combine all of the store survey data from the Sales.Store table into one XML structure, for SalesPersonID 282.
The store survey data is in the Demographics XML column. We also want to include 2 non-XML fields for each store: Name and BusinessEntityID, as node attributes in a parent ‘ Store‘ node. To complete the process, we’ll wrap the final XML structure with a ‘ StoreSurveys‘ root node. Let’s get a 5-record sample from the Sales.Store table, for SalesPersonID 282. GO The store survey XML data ( Demographics column) for the Vinyl and Plastic Goods Corporation store looks like this: To collect all of the store survey data for one sales person into one XML instance, we can again use the FOR XML clause. As with our previous use of FOR XML, the AUTO argument will be the first one we try. Remember that the AUTO argument, by default, gathers every non-XML field into an XML structure in node attribute form.
Like the PATH argument, it also nests any existing XML column data as element nodes. GO The addition of the ROOT argument gives us the top-level wrapper node we wanted, merging all store survey records for the sales person into one XML instance with a root node. Separating XML If we were to perform the converse of the previous example, we would need to pull out the individual ‘ StoreSurvey‘ node block from the combined store surveys XML instance (shown above), for each store name or ID. Separating out chunks of XML data into logical records essentially involves the same procedures we use when shredding XML, but in this case we will preserve of a portion of the XML structure. To demonstrate, let’s combine the store surveys for SalesPersonID 282 again; this time, however, we will use an XML variable to store the combined store surveys.
GO The new Demographics XML structure for BusinessEntityID 312: Note that we did not use CROSS APPLY this time, since the XML data was in a variable instead of an XML column in a table. Also, we used the ‘.‘, a abbreviation, in the query method’s path expression – to pull out the store survey data.

This indicates that the XML portion should be extracted from the nodes method’s node set (the ‘ Store ‘ node), whereas a ‘ /‘ root path expression would have caused the XML to be extracted from the root node (thus returning the entire XML structure). Also, notice that we used the abbreviation ‘.‘ in order to reference attribute values ‘ Name‘ and ‘ ID‘ that are in the node above the ‘ StoreSurvey‘ node (the parent ‘ Store‘ node). A variation on the script that does not require a namespace declaration could be written as follows. GO The above script uses the axis to drill down one level further than the nodes method’s node set, thereby eliminating the need to reference the ‘ StoreSurvey‘ node directly. Summary We’ve taken a look at some ways to coerce XML data to fit specific needs.
We handled one common request, creating XML instances from existing tables, by using the FOR XML clause – applying appropriate arguments to design the XML structure to fit specific aesthetic requirements. We demonstrated another very common procedure, shredding XML data, by employing the XQuery nodes method. We saw that multiple XML fragments and instances can also be merged into a single instance by using the FOR XML tools. We then reversed that operation by splitting the XML instance into logical records of XML data. Manipulating XML data to fit your needs may take creativity and some experimentation with new tools.
We’ve worked out solutions for a few basic problems, but there is more to learn. The techniques that we have introduced here should help to get you started. How to shred children nodes? This is a great article and I learned a lot from you. Thanks for sharing. I have a challenge shredding child nodes and I hope you can shred some light. I added a FamilyInformation and Education child nodes from your same XML.
How can I shred the child nodes elements like TotalChildren, MarriedDate, Highschool, MiddleSchool information from XQuery? How to shred children nodes? This is a great article and I learned a lot from you. Thanks for sharing. I have a challenge shredding child nodes and I hope you can shred some light. I added a FamilyInformation and Education child nodes from your same XML.
Using Variable In Xml File
How can I shred the child nodes elements like TotalChildren, MarriedDate, Highschool, MiddleSchool information from XQuery? Creating XML and writing to the file system Excellent article. I wish I’d found it six-odd months ago when I was starting on the current project. We need to communicate with an external system using XML messages stored on the file system. I eventually managed to work out how to generate the XML I needed but was completely defeated by the problem of how to get this data out into a file.
All the mechanisms I could find (I’m still pretty much a T-SQL newbie now, I was a complete virgin back then) seemed to require a text variable type that was limited to 8000 characters or so, and XML can outgrow that very easily if you turn your back. The only mechanism I could find to get data from a T-SQL variable of type XML out onto the file system was to use an SSIS package, feed the variable to an XSLT transform that did practically nothing, then and specify output to a file. Unfortunately it took me too long to work this out so the project went ahead using 'arms length' methods.
Is there a better way? Creating XML and writing to the file system Excellent article. I wish I’d found it six-odd months ago when I was starting on the current project.
We need to communicate with an external system using XML messages stored on the file system. I eventually managed to work out how to generate the XML I needed but was completely defeated by the problem of how to get this data out into a file. All the mechanisms I could find (I’m still pretty much a T-SQL newbie now, I was a complete virgin back then) seemed to require a text variable type that was limited to 8000 characters or so, and XML can outgrow that very easily if you turn your back. The only mechanism I could find to get data from a T-SQL variable of type XML out onto the file system was to use an SSIS package, feed the variable to an XSLT transform that did practically nothing, then and specify output to a file. Unfortunately it took me too long to work this out so the project went ahead using 'arms length' methods. Is there a better way?
Returning resultset from XML when structure is not known at compile time Hi, I have stored a 'flat' resultset as XML in an XML column and now I need to query the XML and return the original resultset. At runtime when extracting the XML back into resultset, I don’t have access to the original resultset’s structure, only the XML representation. Is there a simple syntax to query an unknown XML structure from an XML columns and return a resultset? In Sql, it is 'select.' . Seems like it should be fairly simple when XML is the source, but I have not yet found it to be simple. RE:Returning resultset from XML when structure is not known at compile time Unfortunately, it’s not that simple to shred XML dataif you think about it for a minute, it’s easy to see why — XML data can hold RELATIONAL data for multiple tablesso it’s not really comparable to a SELECT.
from a SQL table. You have to know at least some of the node names if you want to shred it out, like this (the XML column being 'XMLDETAILS'): SELECT XMLDETAILS.value(‘(/Employee/FirstName)1’,’varchar(20)’) as FirstName, XMLDETAILS.value(‘(/Employee/LastName)1’,’varchar(20)’) as LastName, XMLDETAILS.value(‘(/Employee/@ssn)1’,’int’) as SSN FROM EMPLOYEETBL. RE:Returning resultset from XML when structure is not known at compile time Thanks for the reply delcons. I hadn’t thought about the relational nesting problem, but I can see your point.
I did find about 300 lines of code on sql server central dot com that is a very recent (as of 10/2012) attempt to make such an XML shredder. It seems to work great but I don’t dare deploy it to production because I have no clue how or why the damn thing works!
As an alternative, we’ve decided to generate the query syntax to shred the XML dynamically, at the same time we convert the recordset to XML, and save both together in the record. Because of course at that time we do have access to the original table structure, so we know what the XML structure is. But its kind of a new concept for me- saving the data structure along with the data in every record. I thought that was what XML was supposed to prevent the need for!
Thanks + Column naming question Thank you for such a clear concise and practical introduction to using xml in Sql Server, this post made me wonder whether there is a better method than this one (1) to explicitly name the column generated by the third sql script you posted. Thanks in advance and keep up the good work 🙂 (1) select ( — create XML structure using FOR XML AUTO SELECT BusinessEntityID, PersonType, Title, FirstName, MiddleName, LastName, Suffix FROM Person.Person WHERE BusinessEntityID = 10001 FOR XML AUTO, ELEMENTS GO ) as NamedColumn.