水平拆分XML表

时间:2016-09-26 10:24:25

标签: xml xslt

我有一个包含表格的XML文件。我使用的是XSLT 1.0版。我需要在特定行数之后拆分表(7行之后的示例)。下一页应该有相同的标题,数据应该继续。

图: 1. Original Table 2. Table Transform

XML:

for /l %i in (1, 1, 51) do
( 
    (echo @?/sqlpatch/18881811/postinstall.sql
    ) | sqlplus -s CONNECT / AS SYSDBA@orc%i
)

XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl"?>
<TABLE FRAME="ALL">
<TGROUP COLS="3">
<COLSPEC COLNAME="col1" COLWIDTH="5cm"/>
<COLSPEC COLNAME="col2" COLWIDTH="6.00cm"/>
<COLSPEC COLNAME="col3" COLWIDTH="5.91cm"/>
<THEAD>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA><EMPHASIS>TA</EMPHASIS></PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA><EMPHASIS>TB</EMPHASIS></PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA><EMPHASIS>TC</EMPHASIS></PARA></ENTRY>
</ROW>
</THEAD>
<TBODY>
<ROW>
<ENTRY COLSEP="1" MOREROWS="13" ROWSEP="1"><PARA>A</PARA></ENTRY>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>B</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>C</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A1</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B1</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A2</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>B2</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A4</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A5</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A5<PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A6</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A6</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A7</PARA></ENTRY>
</ROW>
<ROW>

<ENTRY COLSEP="1" ROWSEP="1"><PARA>A7</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A8</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A8</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A9</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A9</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A10</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A10</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A11</PARA></ENTRY>
</ROW>
<ROW>
<ENTRY COLSEP="1" ROWSEP="1"><PARA>A11</PARA></ENTRY>
<ENTRY COLSEP="0" ROWSEP="1"><PARA>A12</PARA></ENTRY>
</ROW>
</TBODY>
</TGROUP>
</TABLE>

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="prowLimit" select="10"/>

  <xsl:variable name="vartable" select="/*"/>

  <xsl:template match="node()|@*" name="identity">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="TABLE/ROW">
    <xsl:if test="position() mod $prowLimit = 7">

      <TBODY>
        <xsl:copy-of select="$vartable/@*"/>
        <xsl:copy-of select=". | following-sibling::ROW[not(position() > $prowLimit )]"/>
      </TBODY>


    </xsl:if>
  </xsl:template>
  </xsl:stylesheet>

但问题是我不确定所需的输出是否正确,因为我不确定它是否遵循XML层次结构。我将按照最终结果来实现,如图所示。

我使用了Dimitre Novatchev在Split large table into several smaller tables中提供的概念。我得到XML解析器错误,以使用我的XML维护XML的结构。

如何修改XSLT以便我可以获得“表变换”图中显示的输出?

1 个答案:

答案 0 :(得分:0)

你想做这样的事情:

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="head" select="TGROUP/COLSPEC | TGROUP/THEAD" />
    <xsl:for-each select="TGROUP/TBODY/ROW[position() mod 7 = 1]">
        <TABLE>
            <xsl:copy-of select="ancestor::TABLE/@*"/>
            <TGROUP>
                <xsl:copy-of select="ancestor::TGROUP/@*"/>
                <xsl:copy-of select="$head"/>
                <TBODY>
                    <xsl:copy-of select=". | following-sibling::ROW[position() &lt; 7]"/>
                </TBODY>
            </TGROUP>
        </TABLE>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

应用于格式良好的输入,例如:

<强> XML

<TABLE FRAME="ALL">
  <TGROUP COLS="3">
    <COLSPEC COLNAME="col1" COLWIDTH="5cm"/>
    <COLSPEC COLNAME="col2" COLWIDTH="6.00cm"/>
    <COLSPEC COLNAME="col3" COLWIDTH="5.91cm"/>
    <THEAD>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>
            <EMPHASIS>TA</EMPHASIS>
          </PARA>
        </ENTRY>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>
            <EMPHASIS>TB</EMPHASIS>
          </PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>
            <EMPHASIS>TC</EMPHASIS>
          </PARA>
        </ENTRY>
      </ROW>
    </THEAD>
    <TBODY>
      <ROW>
        <ENTRY COLSEP="1" MOREROWS="13" ROWSEP="1">
          <PARA>A</PARA>
        </ENTRY>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>B</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>C</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A1</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>B1</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A2</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>B2</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A4</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A5</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A5</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A6</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A6</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A7</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A7</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A8</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A8</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A9</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A9</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A10</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A10</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A11</PARA>
        </ENTRY>
      </ROW>
      <ROW>
        <ENTRY COLSEP="1" ROWSEP="1">
          <PARA>A11</PARA>
        </ENTRY>
        <ENTRY COLSEP="0" ROWSEP="1">
          <PARA>A12</PARA>
        </ENTRY>
      </ROW>
    </TBODY>
  </TGROUP>
</TABLE>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<TABLE FRAME="ALL">
   <TGROUP COLS="3">
      <COLSPEC COLNAME="col1" COLWIDTH="5cm"/>
      <COLSPEC COLNAME="col2" COLWIDTH="6.00cm"/>
      <COLSPEC COLNAME="col3" COLWIDTH="5.91cm"/>
      <THEAD>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>
                  <EMPHASIS>TA</EMPHASIS>
               </PARA>
            </ENTRY>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>
                  <EMPHASIS>TB</EMPHASIS>
               </PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>
                  <EMPHASIS>TC</EMPHASIS>
               </PARA>
            </ENTRY>
         </ROW>
      </THEAD>
      <TBODY>
         <ROW>
            <ENTRY COLSEP="1" MOREROWS="13" ROWSEP="1">
               <PARA>A</PARA>
            </ENTRY>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>B</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>C</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A1</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>B1</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A2</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>B2</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A4</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A5</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A5</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A6</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A6</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A7</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A7</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A8</PARA>
            </ENTRY>
         </ROW>
      </TBODY>
   </TGROUP>
</TABLE>
<TABLE FRAME="ALL">
   <TGROUP COLS="3">
      <COLSPEC COLNAME="col1" COLWIDTH="5cm"/>
      <COLSPEC COLNAME="col2" COLWIDTH="6.00cm"/>
      <COLSPEC COLNAME="col3" COLWIDTH="5.91cm"/>
      <THEAD>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>
                  <EMPHASIS>TA</EMPHASIS>
               </PARA>
            </ENTRY>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>
                  <EMPHASIS>TB</EMPHASIS>
               </PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>
                  <EMPHASIS>TC</EMPHASIS>
               </PARA>
            </ENTRY>
         </ROW>
      </THEAD>
      <TBODY>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A8</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A9</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A9</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A10</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A10</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A11</PARA>
            </ENTRY>
         </ROW>
         <ROW>
            <ENTRY COLSEP="1" ROWSEP="1">
               <PARA>A11</PARA>
            </ENTRY>
            <ENTRY COLSEP="0" ROWSEP="1">
               <PARA>A12</PARA>
            </ENTRY>
         </ROW>
      </TBODY>
   </TGROUP>
</TABLE>

请注意,输出不是格式良好的XML,因为它没有单个根元素。