I have just returned from an MSDN InTrack session over here in the Microsoft Technology Center about Office Open XML and I wanted to share one of the demos I created for this event with you. The demo app extracts XML data from an XML-mapped WordprocessingML document. Note that this is not the Custom XML technology introduced in Open XML, but the earlier model already present in Office 2003. The one that looks like this:
Now the old model provides one great benefit. If you add new rows to the order-item table, the new rows are automatically mapped according to the XML schema that is attached to the document. One of the downsides is that the data is embedded in the rest of the main document markup which makes it harder to extract compared to the separate storage of Custom XML.
Of course using a little XSLT-trickery you can still obtain a data only copy (or use the Word UI for it, by saving as a 'Word 2003 XML document', which is a bit unfriendly to the users.
If you run the following XSLT against the main document body you can extract the data. Note that there are no data-type checks, and reviewing options in the document will mess things up (it's a demo…). Also line-breaks are not copied from the document, or other special non-printing characters such as tabs. Feel free to improve it and I'll update the XSLT here for the rest of the community.
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl=http://www.w3.org/1999/XSL/Transform xmlns:w=http://schemas.openxmlformats.org/wordprocessingml/2006/main exclude-result-prefixes="w">
<xsl:template match="w:p | w:r | w:tbl | w:tr | w:tc"> <xsl:apply-templates select="w:customXml | w:p | w:r | w:tbl | w:tr | w:tc"/> </xsl:template> <xsl:template match="w:customXml"> <xsl:element name="{@w:element}" namespace="{@w:uri}"> <xsl:choose> <xsl:when test="not(descendant::w:customXml)"> <xsl:value-of select="descendant::text()" /> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="child::node()"/> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:template>
</xsl:stylesheet> |
I'll post the demo-app later together with the rest of the demos.
Hope it helps!
Posted
11-10-2007 20:14
by
Anonymous