Live Wire Blog from Vortx

Coder’s Corner – Extension Data Expanded to XML

In an earlier post, we talked about using the Extension Data field as extra storage space for additional product information. But what if you need a more organized version of that information, in a hierarchy of sorts? Most of the time, that would mean adding an additional table to the database, and adding an additional field in the Admin site so users can store that information. This can be tricky to do, and may also take your site off the upgrade path. Fortunately, we've got a great way to store extensible structured data in the ExtensionData field, but there’s no way to directly manipulate and query text in XSL. This tutorial will show you a quick way to get that extension data out as pure transformable XML. Before reading on, you should already be familiar with XML Packages, and have a basic knowledge of SQL. The Query Let’s select a Product ID and it’s Extension Data: select Product.ProductID, CONVERT(xml, ExtensionData) as ExtensionData from Product for xml path('Product') There are three important things to note here:
  1. CONVERT(xml, ExtensionData) - This reads the text in the ExtensionData field in as an XML fragment instead of text full of “>” and “<”. Be careful, as malformed XML will cause your query to explode!
  2. as ExtensionData - This adds the <ExtensionData> node, with the contents just below.
  3. for xml path('Product') - This outputs the results as an XML document instead of a set of rows and columns. Each row becomes a <Product> element (as we specified with the path(‘Product’) call), and each column becomes a child element.
The XML Package You’ll have to make one tiny change in your XML Package <query name="ExtensionData" rowElementName="Product" retType="xml"> See that retType attribute? That tells AspDotNetStorefront to expect XML data and use it as-is in the runtime document. The Results So, what’s all this get us? Well, if I were to type the following into the ExtensionData field of a product called “Blades”: extensiondata casesize 8 Then the XML for my XML Package comes out like this: extensiondata productID Now I can use the following in my XSLT: Add a case of <xsl:value-of select="/ExtensionData/CaseSize" /> to your cart And it will work as advertised!

2 Responses to “Coder’s Corner – Extension Data Expanded to XML”

  1. Thomas Says:

    I like this solution. However, incorrect XML syntax in the ExtensionData field would cause the site to crash. I’d be worried about end users making typos.

  2. Jason Says:

    Thomas is completely correct in that this technique is not well suited for manual data entry. Our intention, however, was to provide a basis for storing custom product data that can easily be displayed in XML packages, not as a complete solution for end-users. For example, using this technique we’ve developed an internal framework for serializing and deserializing multiple .NET objects into a single Extension Data field. This allows us to safely store, retrieve, and display custom data without the deployment and upgrade hassles of modifying the database schema.

Leave a Reply