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.
Let’s select a Product ID and it’s Extension Data:
Product.ProductID, CONVERT(xml, ExtensionData) as ExtensionData
There are three important things to note here:
The XML Package
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!
as ExtensionData - This adds the <ExtensionData> node, with the contents just below.
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.
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.
So, what’s all this get us? Well, if I were to type the following into the ExtensionData field of a product called “Blades”:
Then the XML for my XML Package comes out like this:
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!