Game Development Reference
In-Depth Information
We can use XSLT to perform this conversion. You can use a web browser to
convert the XML using an XSL document, however, why not just write a simple
command line utility (see Listing 12.6)?
using System.Xml.Xsl;
class xslt
{
static void Main (string[] args)
{
var x = new XslCompiledTransform ();
x.Load(args[0]);
x.Transform(args[1], args[2]);
}
}
Listing 12.6. C# utility to transform XML documents (xslt.cs).
The arguments to this command line utility are the XSL document, the original
XML document, and where the converted document should be saved; for example:
xslt.exe transform.xsl CharacterData.xml xformed.xml .
The XSL document that converts Listing 12.1 to Listing 12.5 is shown in List-
ing 12.7.
<xsl:stylesheet xmlns:usos="urn:schemas -microsoft-com:office
:spreadsheet"
version="1.0"
xmlns:xsl=" http: // www . w3 . org /1999/ XSL / Transform "
xsl:version="1.0">
<xsl:output indent="yes" />
<xsl:template match="/">
<Characters>
<xsl:for-each select="/usos:Workbook/usos:Worksheet
/usos:Table/usos:Row">
<xsl:if test="position()!=1">
<CharacterData>
<xsl:for-each select="usos:Cell">
<xsl:variable name="cellPos" select=
"position()" />
<xsl:element name="{../../usos:Row[1]
/usos:Cell[ ￿ cellPos]/usos:Data}">
<xsl:value-of select="usos:Data" />
</xsl:element>
</xsl:for-each>
</CharacterData>
</xsl:if>
</xsl:for-each>
</Characters>
</xsl:template>
</xsl:stylesheet>
Listing 12.7. XSL transform document (transform.xsl).
Search WWH ::




Custom Search