XSLT将XML元素转换为具有不同行数的HTML表

时间:2019-05-17 03:36:13

标签: html xml xslt

我使用模板匹配来提取元素并将其呈现为HTML表。但是我的当前输出出现问题,因为儿童人数不相等。

在下面的XML中,第二个父级比第一个父级有更多的子级。如果将其转换为HTML表,如何使第4行第1列显示为空白?

我打算首先将所有元素放在<cells>中,然后从那里使用Muenchian分组将其转换为Table(与下面的XSLT分开的代码)。

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Table>
  <Parent>
    <Head>Header 1</Head>
    <Children>
      <Node>Node 1</Node>
      <Node>Node 2</Node>
      <Node>Node 3</Node>
    </Children>
  </Parent>
  <Parent>
    <Head>Header 2</Head>
    <Children>
      <Node>Node 4</Node>
      <Node>Node 5</Node>
      <Node>Node 6</Node>
      <Node>Node 7</Node>
    </Children>
  </Parent>
</Table>

XSLT:

<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="Table">
        <cells>
            <xsl:apply-templates select="Parent[1]" mode="parent">
                <xsl:with-param name="row" select="1"/>
                <xsl:with-param name="col" select="1"/>
            </xsl:apply-templates>  
        </cells>
    </xsl:template>

    <xsl:template match="Parent" mode="parent">
        <xsl:param name="row"/>
        <xsl:param name="col"/>

        <xsl:apply-templates select="Children/Node[1]" mode="child">
            <xsl:with-param name="row" select="$row"/>
            <xsl:with-param name="col" select="$col"/>
        </xsl:apply-templates>

        <xsl:apply-templates select="following-sibling::*[1]" mode="parent">
            <xsl:with-param name="row" select="$row"/>
            <xsl:with-param name="col" select="$col + 1"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="Node" mode="child">
        <xsl:param name="row"/>
        <xsl:param name="col"/>

        <cell row="{$row}" col="{$col}">
            <xsl:value-of select="."/>
        </cell>
        <xsl:apply-templates select="following-sibling::*[1]" mode="child">
            <xsl:with-param name="row" select="$row + 1"/>
            <xsl:with-param name="col" select="$col"/>
        </xsl:apply-templates>  
    </xsl:template>
</xsl:stylesheet>

预期的输出(单元格):

<cells>
   <cell row="1" col="1">Node 1</cell>
   <cell row="2" col="1">Node 2</cell>
   <cell row="3" col="1">Node 3</cell>
   <cell row="4" col="1"> </cell>
   <cell row="1" col="2">Node 4</cell>
   <cell row="2" col="2">Node 5</cell>
   <cell row="3" col="2">Node 6</cell>
   <cell row="4" col="2">Node 7</cell>
</cells>

预期的HTML表格:

<table border="1">
   <tr>
      <td>Node 1</td>
      <td>Node 4</td>
   </tr>
   <tr>
      <td>Node 2</td>
      <td>Node 5</td>
   </tr>
   <tr>
      <td>Node 3</td>
      <td>Node 6</td>
   </tr>
   <tr>
      <td> </td>
      <td>Node 7</td>
   </tr>
</table>

2 个答案:

答案 0 :(得分:1)

我将从确定孩子最多的父母开始。然后,为每个父级,为子级最多的每个父级子级创建一个单元格,并用当前父级的相应子级的值填充它:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="parent-by-id" match="Parent" use="generate-id()" />

<xsl:template match="/Table">
    <!-- find the parent with most children -->
    <xsl:variable name="max-parent-id">
        <xsl:for-each select="Parent">
            <xsl:sort select="count(Children/Node)" data-type="number" order="descending"/>
            <xsl:if test="position()=1">
                <xsl:value-of select="generate-id()" />
            </xsl:if>
        </xsl:for-each>
    </xsl:variable>
    <!-- output -->
     <cells>
         <xsl:for-each select="Parent">
            <xsl:variable name="current-parent" select="." />
            <xsl:variable name="i" select="position()" />
            <xsl:for-each select="key('parent-by-id', $max-parent-id)/Children/Node">
                <xsl:variable name="j" select="position()" />
                <cell row="{$j}" col="{$i}">
                    <xsl:value-of select="$current-parent/Children/Node[$j]"/>
                </cell>
            </xsl:for-each>
        </xsl:for-each>
    </cells>
</xsl:template>

</xsl:stylesheet>

已添加:

鉴于编辑后的问题中预期的HTML输出,您可以将操作顺序更改为:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/Table">
    <xsl:variable name="all-parents" select="Parent" />
    <!-- find the parent with most children -->
    <xsl:for-each select="Parent">
        <xsl:sort select="count(Children/Node)" data-type="number" order="descending"/>
        <xsl:if test="position()=1">
            <!-- output -->
            <table border="1">
                <xsl:for-each select="Children/Node">
                    <xsl:variable name="i" select="position()" />
                    <tr>
                        <xsl:for-each select="$all-parents">
                            <td>
                                <xsl:value-of select="Children/Node[$i]" />
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </table>
        </xsl:if>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

并直接获得预期的输出,而无需中间阶段。

答案 1 :(得分:0)

这可能不是最优雅的代码,但是我能够按预期呈现HTML。如果您对如何使此代码更简洁,更高效有任何建议,请告诉我。谢谢!

<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="Table">
        <xsl:for-each select="Parent/Children">
             <xsl:sort select="count(Node)" data-type="number" order="descending"/>
             <xsl:if test="position() = 1">
                <cells>
                    <xsl:apply-templates select="../../Parent[1]" mode="parent">
                        <xsl:with-param name="row" select="1"/>
                        <xsl:with-param name="col" select="1"/>
                        <xsl:with-param name="max-rows" select="count(Node)"/>
                    </xsl:apply-templates>  
                </cells>
             </xsl:if>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="Parent" mode="parent">
        <xsl:param name="row"/>
        <xsl:param name="col"/>
        <xsl:param name="max-rows"/>

        <xsl:apply-templates select="Children/Node[1]" mode="child">
            <xsl:with-param name="row" select="$row"/>
            <xsl:with-param name="col" select="$col"/>
            <xsl:with-param name="max-rows" select="$max-rows"/>
        </xsl:apply-templates>

        <xsl:apply-templates select="following-sibling::*[1]" mode="parent">
            <xsl:with-param name="row" select="$row"/>
            <xsl:with-param name="col" select="$col + 1"/>
            <xsl:with-param name="max-rows" select="$max-rows"/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="Node" mode="child">
        <xsl:param name="row"/>
        <xsl:param name="col"/>
        <xsl:param name="max-rows"/>

        <cell row="{$row}" col="{$col}">
            <xsl:value-of select="."/>
        </cell>
        <xsl:choose>
            <xsl:when test="following-sibling::*">
                <xsl:apply-templates select="following-sibling::*[1]" mode="child">
                    <xsl:with-param name="row" select="$row + 1"/>
                    <xsl:with-param name="col" select="$col"/>
                    <xsl:with-param name="max-rows" select="$max-rows"/>
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
                 <xsl:call-template name="Placeholder">
                    <xsl:with-param name="row" select="$row + 1"/>
                    <xsl:with-param name="col" select="$col"/>
                    <xsl:with-param name="max-rows" select="$max-rows"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <xsl:template name='Placeholder'>
        <xsl:param name="row"/>
        <xsl:param name="col"/>
        <xsl:param name="max-rows"/>

        <xsl:if test="$row &lt;= $max-rows">
            <cell row="{$row}" col="{$col}">
            </cell>
            <xsl:call-template name="Placeholder">
                <xsl:with-param name="row" select="$row + 1"/>
                <xsl:with-param name="col" select="$col"/>
                <xsl:with-param name="max-rows" select="$max-rows"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>
相关问题