- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
In a previous guide entry, I've told you about some of the various versions of RSS, an XML format used for syndicating content. Today I'm going to talk about transforming an XML document created by SQL Server into RSS using Visual Basic .NET.
Let's start by looking at the source. Assume, for example, that we have a database table that looks like this:
CREATE TABLE RSSFeed ( ItemID int identity (1, 1) NOT NULL , ItemName varchar (100) NULL , PublishDate smalldatetime NULL , ItemDesc varchar (500) NULL , ItemURL varchar(100) NULL )
If we were to do a query on SQL Server, we might (depending on the options we choose) get a document something like this:
<ROOT> <RSSFeed ItemID="c388ad07-8f55-438b-a587-a19b75a052ba" ItemName="Blog :: New Sun Web Services Developer Title Now Live" PublishDate="July 30, 2004" ItemDesc="So you can build web services applications. Prove it. Now you can, with..." ItemURL="http://www.informit.com/discussion/index.asp?postid=c388ad07-8f55-438b-a587-a19b75a052ba" /> <RSSFeed ItemID="3c08e7f7-49d3-4487-8fe5-82953c633e6c" ItemName="Blog :: What's so great about Grid?" PublishDate="July 28, 2004" ItemDesc="I'm glad to see that grid computing is starting to pop up more frequently. In fact, ..." ItemURL="http://www.informit.com/discussion/index.asp?postid=3c08e7f7-49d3-4487-8fe5-82953c633e6c" /> </ROOT>
We could, if we so desired, use XSL Transformations to turn this into any number of RSS formats. (In fact, we could do all nine, if we were that ambitious.) For example, we could turn this format into a simple RSS .91 feed with this stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <rss version="2.0"> <channel> <title>InformIT :: XML Reference Guide</title> <link>http://www.informit.com/</link> <description>The latest weblog and content updates for the InformIT-XML Reference Guide</description> <language>en</language> <xsl:apply-templates /> </channel> </rss> </xsl:template> <xsl:template match="RSSFeed"> <item> <title><xsl:value-of select="@ItemName" /></title> <link><xsl:value-of select="@ItemURL" /></link> <description><xsl:value-of select="@ItemDesc" /></description> </item> </xsl:template> </xsl:stylesheet>
But how do we actually perform the transformation in .NET? It turns out to be remarkably straightforward.
If you're already familiar with creating .NET applications, skip ahead to the next paragraph. For those of you who are not, stay with me here:
- Create a text file called
TranslateRSS.vb
. (We'll populate it in a moment.) - Create a script file called
buildTrans.bat
and add the following text:vbc.exe /t:exe /debug+ /optionstrict+ /out:.\TranslateRSS.exe TranslateRSS.vb /r:System.dll,System.Xml.dll
- Save
buildTrans.bat
in the same directory asTranslateRSS.vb
. - When you're ready, run
buildTrans.bat
to build the VB.NET file into an application,TranslateRSS.exe
. - Run
TranslateRSS.exe
to see the results.
OK, now that we've taken care of the mechanics, let's look at the application itself. The framework is easy:
Option strict off Imports Microsoft.VisualBasic Imports System Imports System.IO public class TransformXMLSample shared sub Main() Console.WriteLine ("Transformation will go here.") end sub end Class
Now we can add the actual transformation:
Option strict off Imports Microsoft.VisualBasic Imports System Imports System.IO Imports System.Xml Imports System.Xml.Xsl public class TransformXMLSample shared sub Main() Dim xslt As New XslTransform() Dim resolver as XmlUrlResolver = new XmlUrlResolver() xslt.Load(CType("rss.xsl", String)) xslt.Transform("rssdata.xml", "rssout.xml", resolver) end sub end Class
Note that I'm assuming that the database information has been written to a file. You could just as easily output it to a stream and use the stream as the input for the transformation.
The result is an RSS 2.0 file:
<rss version="2.0"> <channel> <title>InformIT :: XML Reference Guide</title> <link>http://www.informit.com/ <description>The latest weblog and content updates for the InformIT-XML Reference Guide</description> <language>en</language> <item> <title>Blog :: New Sun Web Services Developer Title Now Live</title> <link>http://www.informit.com/discussion/index.asp?postid=c388ad07-8f55-438b-a587-a19b75a052ba <description>So you can build web services applications. Prove it. Now you can, with...</description> </item> <item> <title>Blog :: What's so great about Grid?</title> <link>http://www.informit.com/discussion/index.asp?postid=3c08e7f7-49d3-4487-8fe5-82953c633e6c <description>I'm glad to see that grid computing is starting to pop up more frequently. In fact, ...</description> </item> </channel> </rss>
The beauty of this approach is that you can actually provide your readers with a variety of choices by simply adding subsequent transofmraitons that transform the data into a variety of formats, including Atom, leaving all of them for readers to choose or toss aside.