Using an xsl stylesheet to transform a generic xml document into a dita document
This transform shows the conversion of a generic xml into a dita document, but can be generalized.
Note that to maintain all of the original tags, we use the Identity Transformation ». This basically copies all of your tags and attributes, and then you can use templates to convert the tags you want to change.
Original XML document
<?xml version="1.0" encoding="iso-8859-1"?>
<document>
<tit>A very nice title</tit>
<body>
<include src="an_id" />
<img src="picture.png" alt="hello" />
<example>Here is a beautiful example.</example>
<description>What about this description?</description>
<p>Got to <mi>Start</mi> then to <mi>Run</mi> and type <mi>cmd</mi>.</p>
<p>Search with <a href="http://www.google.com">Google</a>.</p>
<p>This is a custom <link href="link_to_me">link</link>.</p>
<code><?php phpinfo(); ?></code>
<p>That's all folks.</p>
</body>
</document>
<document>
<tit>A very nice title</tit>
<body>
<include src="an_id" />
<img src="picture.png" alt="hello" />
<example>Here is a beautiful example.</example>
<description>What about this description?</description>
<p>Got to <mi>Start</mi> then to <mi>Run</mi> and type <mi>cmd</mi>.</p>
<p>Search with <a href="http://www.google.com">Google</a>.</p>
<p>This is a custom <link href="link_to_me">link</link>.</p>
<code><?php phpinfo(); ?></code>
<p>That's all folks.</p>
</body>
</document>
XSL stylesheet
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" version="1.0" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<topic>
<xsl:apply-templates select="/document/*" />
</topic>
</xsl:template>
<xsl:template match="tit">
<xsl:element name="title">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="include">
<xsl:element name="section">
<xsl:attribute name="conref">
<xsl:value-of select="./@src" />
</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="example">
<xsl:element name="pre">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="description">
<xsl:element name="alt">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="mi">
<xsl:element name="uicontrol">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="a">
<xsl:element name="xref">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="link">
<xsl:element name="xref">
<xsl:attribute name="href">
<xsl:value-of select="@docid" />
</xsl:attribute>
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="code">
<xsl:element name="tt">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="image">
<xsl:element name="image">
<xsl:attribute name="placement">
<xsl:text>break</xsl:text>
</xsl:attribute>
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" version="1.0" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<topic>
<xsl:apply-templates select="/document/*" />
</topic>
</xsl:template>
<xsl:template match="tit">
<xsl:element name="title">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="include">
<xsl:element name="section">
<xsl:attribute name="conref">
<xsl:value-of select="./@src" />
</xsl:attribute>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="example">
<xsl:element name="pre">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="description">
<xsl:element name="alt">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="mi">
<xsl:element name="uicontrol">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="a">
<xsl:element name="xref">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="link">
<xsl:element name="xref">
<xsl:attribute name="href">
<xsl:value-of select="@docid" />
</xsl:attribute>
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="code">
<xsl:element name="tt">
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="image">
<xsl:element name="image">
<xsl:attribute name="placement">
<xsl:text>break</xsl:text>
</xsl:attribute>
<xsl:copy-of select="attribute::node()" />
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output of the transformation
<?xml version="1.0" encoding="utf-8"?>
<topic>
<title>A very nice title</title>
<body>
<section conref="an_id"/>
<img src="picture.png" alt="hello"/>
<pre>Here is a beautiful example.</pre>
<alt>What about this description?</alt>
<p>Got to <uicontrol>Start</uicontrol> then to <uicontrol>Run</uicontrol> and type <uicontrol>cmd</uicontrol>.</p>
<p>Search with <xref href="http://www.google.com">Google</xref>.</p>
<p>This is a custom <xref href="link_to_me">link</xref>.</p>
<tt><?php phpinfo(); ?></tt>
<p>That's all folks.</p>
</body>
</topic>
<topic>
<title>A very nice title</title>
<body>
<section conref="an_id"/>
<img src="picture.png" alt="hello"/>
<pre>Here is a beautiful example.</pre>
<alt>What about this description?</alt>
<p>Got to <uicontrol>Start</uicontrol> then to <uicontrol>Run</uicontrol> and type <uicontrol>cmd</uicontrol>.</p>
<p>Search with <xref href="http://www.google.com">Google</xref>.</p>
<p>This is a custom <xref href="link_to_me">link</xref>.</p>
<tt><?php phpinfo(); ?></tt>
<p>That's all folks.</p>
</body>
</topic>
References
- http://en.wikipedia.org/wiki/Identity_transform »
- http://www.zvon.org »
- http://www.w3.org/TR/xslt »
- http://www.w3schools.com/xsl/ »
[Click to add or edit comments])
Please prepend comments below including a date