这种转变:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/*/*/*/Table"/>
</xsl:template>
<xsl:template match="Table">
<table>
<xsl:apply-templates select="node()|@*"/>
</table>
</xsl:template>
<xsl:template match="TableHead">
<th><xsl:apply-templates select="node()|@*"/></th>
</xsl:template>
<xsl:template match="tr[not(th)][position() mod 2 = 1]">
<tr class="odd">
<xsl:apply-templates select="node()|@*"/>
</tr>
</xsl:template>
<xsl:template match="tr[not(th)][position() mod 2 = 0]">
<tr class="even">
<xsl:apply-templates select="node()|@*"/>
</tr>
</xsl:template>
</xsl:stylesheet>
应用于提供的 XML 文档时:
<Item TextType="BodyMatter" SchemaVersion="2.0" id="AaAF_Freelance_Test_1" DiscussionAlias="Discussion" SessionAlias="Chapter" SecondColour="Pantone326C" ThirdColour="Pantone2945C" FourthColour="Pantone272C" Logo="colour" Rendering="VLE Preview" Template="Wide_Margin_Reduced_A4_Unnumbered_new_design_v10_release1">
<CourseCode>AaAF</CourseCode>
<CourseTitle>Accessible and alternative formats</CourseTitle>
<ItemID/>
<ItemTitle>AaAF: Freelance XSLT test document 1</ItemTitle>
<Unit>
<UnitID/>
<UnitTitle/>
<ByLine/>
<Session>
<Title>Freelance XSLT test – constructing tables</Title>
<Paragraph>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec quis elit sapien. Duis sit amet interdum lectus. Sed faucibus, mi quis dapibus consequat, nisi nunc semper erat, eu pretium enim urna porta diam. Suspendisse mi nisi, adipiscing in semper ac, viverra vitae nisl. Nulla facilisi. Nullam luctus porttitor velit, quis euismod dui commodo eu. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris arcu est, rhoncus interdum bibendum vel, commodo et justo.</Paragraph>
<Table>
<TableHead>Table of analytical techniques</TableHead>
<tbody>
<tr>
<th class="ColumnHeadLeft">Analytical technique</th>
<th class="ColumnHeadLeft">Application</th>
</tr>
<tr>
<td class="TableLeft">IR reflectography</td>
<td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td>
</tr>
<tr>
<td class="TableLeft">FT-IR spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations.
</td>
</tr>
<tr>
<td class="TableLeft">Raman spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings.
</td>
</tr>
<tr>
<td class="TableLeft">XRF</td>
<td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td>
</tr>
<tr>
<td class="TableLeft">Vis-NIR spectroscopy</td>
<td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td>
</tr>
<tr>
<td class="TableLeft">UV-vis spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of the presence of organic substances.
</td>
</tr>
<tr>
<td class="TableLeft">Fluorimetry</td>
<td class="TableLeft">
<i>In situ</i> detection of the distribution of organic molecules
</td>
</tr>
<tr>
<td class="TableLeft">Drilling resistance measurement </td>
<td class="TableLeft">Hardness of rocks used for monuments and sculptures</td>
</tr>
</tbody>
</Table>
</Session>
</Unit>
</Item>
产生想要的正确结果:
<table>
<th>Table of analytical techniques</th>
<tbody>
<tr>
<th class="ColumnHeadLeft">Analytical technique</th>
<th class="ColumnHeadLeft">Application</th>
</tr>
<tr class="odd">
<td class="TableLeft">IR reflectography</td>
<td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td>
</tr>
<tr class="even">
<td class="TableLeft">FT-IR spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations.
</td>
</tr>
<tr class="odd">
<td class="TableLeft">Raman spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings.
</td>
</tr>
<tr class="even">
<td class="TableLeft">XRF</td>
<td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td>
</tr>
<tr class="odd">
<td class="TableLeft">Vis-NIR spectroscopy</td>
<td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td>
</tr>
<tr class="even">
<td class="TableLeft">UV-vis spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of the presence of organic substances.
</td>
</tr>
<tr class="odd">
<td class="TableLeft">Fluorimetry</td>
<td class="TableLeft">
<i>In situ</i> detection of the distribution of organic molecules
</td>
</tr>
<tr class="even">
<td class="TableLeft">Drilling resistance measurement </td>
<td class="TableLeft">Hardness of rocks used for monuments and sculptures</td>
</tr>
</tbody>
</table>
请注意:
转换只是为每个添加了“奇数”或“偶数”的类属性tr
。这假定一个 css 文件也将被引用,并且它具有为.odd
和定义的适当的 CSS 属性.even
。
如果不是这种情况,则可以更改转换以生成直接格式。
例如,我们可以分别在两个模板中:
<tr bgcolor="white">
和
<tr bgcolor="#87b1f1">
最后,如果我们希望输出位于 XHTML 命名空间中,我们将进一步修改转换,如下所示:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/*/*/*/Table"/>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="http://www.w3.org/1999/xhtml">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="Table">
<table>
<xsl:apply-templates select="node()|@*"/>
</table>
</xsl:template>
<xsl:template match="TableHead">
<th><xsl:apply-templates select="node()|@*"/></th>
</xsl:template>
<xsl:template match="tr[not(th)][position() mod 2 = 1]">
<tr bgcolor="white">
<xsl:apply-templates select="node()|@*"/>
</tr>
</xsl:template>
<xsl:template match="tr[not(th)][position() mod 2 = 0]">
<tr bgcolor="#87b1f1">
<xsl:apply-templates select="node()|@*"/>
</tr>
</xsl:template>
</xsl:stylesheet>
当使用此转换处理相同的 XML 文档时,结果现在位于 XHTML 命名空间中:
<table xmlns="http://www.w3.org/1999/xhtml">
<th>Table of analytical techniques</th>
<tbody>
<tr>
<th class="ColumnHeadLeft">Analytical technique</th>
<th class="ColumnHeadLeft">Application</th>
</tr>
<tr bgcolor="white">
<td class="TableLeft">IR reflectography</td>
<td class="TableLeft">Characterisation of painting techniques, in panel and wall paintings, through the identification of underdrawings; identification of undocumented interventions; characterisation of pigments.</td>
</tr>
<tr bgcolor="#87b1f1">
<td class="TableLeft">FT-IR spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of alterations on stones, mortars and metals, identification of pigments and binders in paintings; identification of undocumented restorations.
</td>
</tr>
<tr bgcolor="white">
<td class="TableLeft">Raman spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of alterations on stones, mortars and metals; identification of pigments in wall paintings.
</td>
</tr>
<tr bgcolor="#87b1f1">
<td class="TableLeft">XRF</td>
<td class="TableLeft">Elemental analyses of stones, metals, wall and easel paintings, and other objects. </td>
</tr>
<tr bgcolor="white">
<td class="TableLeft">Vis-NIR spectroscopy</td>
<td class="TableLeft">Colorimetric measurements and, possibly, identification of pigments. Measurement of possible colour changing after restoration.</td>
</tr>
<tr bgcolor="#87b1f1">
<td class="TableLeft">UV-vis spectroscopy</td>
<td class="TableLeft">
<i>In situ</i> non-destructive identification of the presence of organic substances.
</td>
</tr>
<tr bgcolor="white">
<td class="TableLeft">Fluorimetry</td>
<td class="TableLeft">
<i>In situ</i> detection of the distribution of organic molecules
</td>
</tr>
<tr bgcolor="#87b1f1">
<td class="TableLeft">Drilling resistance measurement </td>
<td class="TableLeft">Hardness of rocks used for monuments and sculptures</td>
</tr>
</tbody>
</table>