如何使用XSLT将此XML转换为表

如何使用XSLT将此XML转换为表,xml,xslt,Xml,Xslt,请建议如何为以下XML编写XSLT: <?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="list8.xsl"?> <SeminarDetails> <CourseName>XML Introduction</CourseName> <Code>1234</Code> <Content1> <Chapte

请建议如何为以下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

给定您的输入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&#x9;Content1&#x9;Content2&#xa;</xsl:text>
    <xsl:value-of select="Code"/>
    <xsl:for-each select="Content1/*">
      <xsl:text>&#x9;</xsl:text>
      <xsl:variable name="posContent1" select="position()"/>
      <xsl:value-of select="."/>
      <xsl:text>&#x9;</xsl:text>
      <xsl:value-of select="../../Content2/*[position() = $posContent1]"/>
      <xsl:text>&#xa;</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>&#x9;</xsl:text>
      <xsl:text>&#x9;</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#xa;</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   
粘贴到电子表格时显示如下:

<?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&#x9;Content1&#x9;Content2&#xa;</xsl:text>
    <xsl:value-of select="Code"/>
    <xsl:for-each select="Content1/*">
      <xsl:text>&#x9;</xsl:text>
      <xsl:variable name="posContent1" select="position()"/>
      <xsl:value-of select="."/>
      <xsl:text>&#x9;</xsl:text>
      <xsl:value-of select="../../Content2/*[position() = $posContent1]"/>
      <xsl:text>&#xa;</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>&#x9;</xsl:text>
      <xsl:text>&#x9;</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>&#xa;</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   

我相信这会更加优雅-尤其是如果您可以使用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&#9;Content1&#9;Content2&#10;</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>&#9;</xsl:text>   
        <xsl:value-of select="$col-a[$i]"/>
        <xsl:text>&#9;</xsl:text>   
        <xsl:value-of select="$col-b[$i]"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

代码	;内容1和内容9;内容2和10;
	   
	   



将输出复制到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>

TD{垂直对齐:顶部}


您想要文本还是HTML输出?我需要文本输出…请说明XSLT 1.0或2.0。嗨,Michael,我对XSLT版本不太了解。我想应该是1.0。请指导我。了解XSLT的强大功能并从中获益也很有用。这将允许您将第一个和最后一个模板作为冗余删除。您的第三个模板可能只应该
match=“Chapter1 | Chapter2”
,因为我看不出在课程名称或代码中添加换行符有什么好处。-除此之外,你是在开玩笑吧?OP在评论中明确指出(在被询问后),输出应该是文本,而不是HTML。但是,随着事情的发展,你的答案很可能最终会被接受。