Xml 每个循环转换的XSLT

Xml 每个循环转换的XSLT,xml,xslt,Xml,Xslt,我刚刚接到一项任务,要转换一些数据,但不幸的是,我以前没有接触过XSLT,因此我对这些概念相当陌生。我已经能够转换静态内容,但我被要求的文件包含具有不同行数和列数的类似于表的结构 我最终试图实现的是相同的东西,但被翻译成HTML 下面是我需要翻译的伪XML示例 <document> <item> <richtext> <rubbishToIgnore> Don't include this </rubbishToIgnore

我刚刚接到一项任务,要转换一些数据,但不幸的是,我以前没有接触过XSLT,因此我对这些概念相当陌生。我已经能够转换静态内容,但我被要求的文件包含具有不同行数和列数的类似于表的结构

我最终试图实现的是相同的东西,但被翻译成HTML

下面是我需要翻译的伪XML示例

<document>
<item> 
    <richtext>
    <rubbishToIgnore> Don't include this </rubbishToIgnore>
        <mytable>
            <myrow>
            <rubbishToIgnore> Don't include this </rubbishToIgnore>
            <rubbishToIgnore> Don't include this </rubbishToIgnore>
                <mycol colspan="2" >Data 1 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 2 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 3 </mycol>
            </myrow>
        </mytable>
        <mytable>
            <myrow>
                <mycol colspan="2" >Data 1 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 2 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 3 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 4 </mycol>
            </myrow>
        </mytable>
</item>
<item> 
    <richtext>
    <rubbishToIgnore> Don't include this </rubbishToIgnore>
        <mytable>
            <myrow>
            <rubbishToIgnore> Don't include this </rubbishToIgnore>
            <rubbishToIgnore> Don't include this </rubbishToIgnore>
                <mycol colspan="2" >Data 1 </mycol>
            </myrow>
            <myrow>
                <rubbishToIgnore> Don't include this </rubbishToIgnore>
                <mycol>Data 2 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 3 </mycol>
            </myrow>
        </mytable>
        <mytable>
            <myrow>
                <mycol colspan="2" >Data 1 </mycol>
            </myrow>
            <myrow>
                <mycol>Data 2 </mycol>
            </myrow>
        </mytable>
</item>

不要包括这个
不要包括这个
不要包括这个
数据1
数据2
数据3
数据1
数据2
数据3
数据4
不要包括这个
不要包括这个
不要包括这个
数据1
不要包括这个
数据2
数据3
数据1
数据2

我只是在寻找一些指针/帮助,以正确的方式使用XSLT循环XML并创建一个简单的HTML表

这就是我想到的XSLT伪代码

<xsl:for-each select="document/item>
<xsl:for-each select="document/item/richtext>
<xsl:for-each select="document/item/richtext/mytable">
    <table border = "1">
        <xsl:for-each select="document/item/richtext/myrow">
            <tr>
                <xsl:for-each select="document/item/richtext/mycol">
                    <td>
                        <xsl:value-of select="document/item/richtext/mycol"/>
                    <td>
                </xsl:for-each>
            <tr>
        </xsl:for-each>
    </table>
</xsl:for-each>

与此同时,我将通过阅读W3schools教程来尝试自己解决这个问题


谢谢你们

嵌套的
xsl:for each
语句有问题。第二个不会选择任何东西

<xsl:for-each select="document/item">
  <xsl:for-each select="document/item/richtext">
选择子节点的问题同样适用于
xsl:for each
语句的其余部分。例如,要选择行,您可以这样做

<xsl:for-each select="myrow">

深度嵌套的xsl:for-each指令通常是错误的方法。将其分解为与输入中的不同元素相匹配的模板规则;当处理一个元素时,使用xsl:apply-templates对其子元素调用适当的处理。感谢Tim的帮助和清晰的解释,这真的帮助很大,对我很有用!
<xsl:for-each select="myrow">
<xsl:template match="/">
  <xsl:for-each select="document/item/richtext/mytable">
    <table border = "1">
      <xsl:for-each select="myrow">
        <tr>
          <xsl:for-each select="mycol">
            <td>
              <xsl:value-of select="."/>
            </td>
          </xsl:for-each>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:for-each>
</xsl:template>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes" html-version="5"/>

<xsl:template match="/">
  <xsl:apply-templates select="document/item/richtext/mytable" />
</xsl:template>

<xsl:template match="mytable">
  <table border = "1">
    <xsl:apply-templates select="myrow" />
  </table>
</xsl:template>

<xsl:template match="myrow">
  <tr>
    <xsl:apply-templates select="mycol" />
  </tr>
</xsl:template>

<xsl:template match="mycol">
  <td>
    <xsl:value-of select="."/>
  </td>
</xsl:template>
</xsl:stylesheet>
<xsl:template match="mycol">
  <td>
    <xsl:copy-of select="@colspan" />
    <xsl:value-of select="."/>
  </td>
</xsl:template>