繁体   English   中英

XSLT for-each循环转换

[英]XSLT for-each loop transformation

我刚刚获得了转换某些数据的任务,但是不幸的是,我之前没有接触过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>

我只是在寻找一些指向使用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选择一个item元素,因此嵌套的xsl:for-each语句将相对于该item元素。 换句话说,它正在寻找一个称为document的子元素,该元素显然不存在。

实际上,您根本不需要前两个语句。 您可以只使用第三个元素直接获取元素。

<xsl:for-each select="document/item/richtext/mytable">

选择子节点的相同问题也适用于xsl:for-each语句的其余部分。 因此,例如,要选择行,您可以这样做...

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

试试这个XSLT

<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>

但是,如果您打算做更多的XSLT,则可能要考虑使用使用模板的方法。 并不是for-each语句有任何问题,但是在这种情况下,它将有助于减少嵌套和代码块的大小。

也尝试这个XSLT

<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>

如果要包括colspan属性,请将“ mycol”匹配模板更改为此...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM