请建议如何为以下XML编写XSLT:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="list8.xsl"?>
<SeminarDetails>
<CourseName>XML Introduction</CourseName>
<Code>1234</Code>
<Content1>
<Chapter1>XML Overview</Chapter1>
<Chapter1>XML Document Creation</Chapter1>
<Chapter1>DTD</Chapter1>
<Chapter1>XML Schema</Chapter1>
<Chapter1>XSLT, XPath</Chapter1>
<Chapter1>Namespace</Chapter1>
</Content1>
<Content2>
<Chapter2>XML basics</Chapter2>
<Chapter2>XML Document basics Creation1</Chapter2>
<Chapter2>Description</Chapter2>
<Chapter2>XML Schema1</Chapter2>
</Content2>
</SeminarDetails>
我需要3列,如下所示:
Code Content1 Content2
1234 XML Overview XML basics
XML Document Creation XML Document basics Creation1
DTD Description
XML Schema
XSLT, XPath
答案 0 :(得分:0)
根据您的输入XML文档,以下XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="SeminarDetails">
<xsl:text>Code	Content1	Content2
</xsl:text>
<xsl:value-of select="Code"/>
<xsl:for-each select="Content1/*">
<xsl:text>	</xsl:text>
<xsl:variable name="posContent1" select="position()"/>
<xsl:value-of select="."/>
<xsl:text>	</xsl:text>
<xsl:value-of select="../../Content2/*[position() = $posContent1]"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
<!-- In case Content2 has more children than Content1... -->
<xsl:variable name="countContent1" select="count(Content1/*)"/>
<xsl:for-each select="Content2/*[position() > $countContent1]">
<xsl:text>	</xsl:text>
<xsl:text>	</xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
将生成所需的(制表符分隔)输出:
Code Content1 Content2
1234 XML Overview XML basics
XML Document Creation XML Document basics Creation1
DTD Description
XML Schema XML Schema1
XSLT, XPath
Namespace
粘贴到电子表格时看起来如下所示:
答案 1 :(得分:0)
我相信这可能会更优雅 - 尤其是如果你可以使用 XSLT 2.0 :
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/SeminarDetails">
<!-- header -->
<xsl:text>Code	Content1	Content2 </xsl:text>
<!-- define column contents -->
<xsl:variable name="col-0" select="Code" />
<xsl:variable name="col-a" select="Content1/Chapter1" />
<xsl:variable name="col-b" select="Content2/Chapter2" />
<!-- generate rows -->
<xsl:for-each select="1 to max(((count($col-a)), (count($col-b))))">
<xsl:variable name="i" select="." />
<xsl:value-of select="$col-0[$i]"/>
<xsl:text>	</xsl:text>
<xsl:value-of select="$col-a[$i]"/>
<xsl:text>	</xsl:text>
<xsl:value-of select="$col-b[$i]"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答案 2 :(得分:0)
当要将输出复制到MS-Excel时,制表符分隔输出很有用。然而,当输出要在网站上发布时,知道如何在html中混合输出也是有用的。此外,我总是建议从XSLT的递归功能中受益。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="*">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="SeminarDetails">
<STYLE> TD {vertical-align:top} </STYLE>
<H1><xsl:apply-templates select="CourseName" /></H1>
<TABLE border="1">
<TR>
<TD><xsl:apply-templates select="Code" /></TD>
<TD><xsl:apply-templates select="Content1" /></TD>
<TD><xsl:apply-templates select="Content2" /></TD>
</TR></TABLE>
</xsl:template>
<xsl:template match="CourseName|Code|Chapter1|Chapter2">
<xsl:copy-of select="." /><BR />
</xsl:template>
<xsl:template match="Content1|Content2">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>