使用xslt输入标记层次结构作为表格式

时间:2017-07-28 12:17:23

标签: xml xslt xslt-2.0

如何获取表格格式输入的标记层次结构

我的输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<topic outputclass="TOA_Entries">
   <title outputclass="TOA_E">Table of Authorities – Entries</title>
   <body>
      <table frame="all" outputclass="TableGrid0">
         <tgroup cols="2">
            <colspec colname="col1" colwidth="130.50pt"/>
            <colspec colname="col2" colwidth="245.50pt"/>
            <tbody>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Title">Structures</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L1">Carnivanal Code (26 U.S.C.)</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_S_IRC_ch61" outputclass="TOA_ID">S_IRC_ch61</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">ch. 61</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_S_IRC_IRC1931" outputclass="TOA_ID">S_IRC_IRC1931</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">I.R.C. of 1931</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L1">Investment Company Act of 1940</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L2">Rules</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_S_ICA_R_ICA1940" outputclass="TOA_ID">S_ICA_R_ICA1940</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">202(a)(11)(G)-1</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L1">Securities Act of 1933</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L2">Rules</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_S_SA_R_SA1933" outputclass="TOA_ID">S_SA_R_SA1933</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">202(a)(30)-1(a)</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_S_SA_R_SA1933_1" outputclass="TOA_ID">S_SA_R_SA1933_1</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">202(a)(30)-1(a)(1)</p>
                  </entry>
               </row>
            </tbody>
         </tgroup>
      </table>
      <table frame="all" outputclass="TableGrid0">
         <tgroup cols="2">
            <colspec colname="col1" colwidth="130.50pt"/>
            <colspec colname="col2" colwidth="247.50pt"/>
            <tbody>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Title">Cases</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_C_3MCoVBrowner" outputclass="TOA_ID">C_3MCoVBrowner</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">3M Co. v. Browner</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L1">A</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_C_AbbateVUS" outputclass="TOA_ID">C_AbbateVUS</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">Abbate v. United States</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_C_AbelVUS" outputclass="TOA_ID">C_AbelVUS</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">Abel v. United States</p>
                  </entry>
               </row>
               <row>
                  <entry namest="col1" nameend="col2">
                     <p outputclass="TOA_Text_L1">B</p>
                  </entry>
               </row>
               <row>
                  <entry/>
                  <entry>
                     <p outputclass="TOA_Entry_L1">Bacon v. Comm’r</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_C_BaconVCommr1" outputclass="TOA_ID">C_BaconVCommr1</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L2">September 1, 1982</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_C_BaconVCommr2" outputclass="TOA_ID">C_BaconVCommr2</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L2">January 8, 1984</p>
                  </entry>
               </row>
               <row>
                  <entry>
                     <p id="p_C_CompaqCompVCommr" outputclass="TOA_ID">C_CompaqCompVCommr</p>
                  </entry>
                  <entry>
                     <p outputclass="TOA_Entry_L1">Compaq Computer Corp. v. Comm’r</p>
                  </entry>
               </row>
            </tbody>
         </tgroup>
      </table>
   </body>
</topic>

我正在使用的XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="@* | node()" name="identity"
        priority="1">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']" priority="25">
        <toa type="toa">
            <xsl:apply-templates />
        </toa>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/title[@outputclass='TOA_E']" priority="25">
        <title>
            <xsl:apply-templates />
        </title>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']" priority="118">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup" priority="119">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/colspec" priority="120">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody" priority="121">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row" priority="122">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry" priority="123">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody" priority="124">
        <toa-section>
            <xsl:apply-templates />
        </toa-section>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry/p[@outputclass='TOA_Title']" priority="125">
        <title>
            <xsl:apply-templates />
        </title>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry/p[@outputclass='TOA_Text_L1']" priority="126">
        <toa-div level="div1">
            <title>
                <xsl:apply-templates />
            </title>
        </toa-div>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry/p[@outputclass='TOA_Text_L2']" priority="127">
        <toa-div level="div2">
            <title>
                <xsl:apply-templates />
            </title>
        </toa-div>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry/p[@outputclass='TOA_Entry_L1']" priority="127">
        <xsl:element name="toa-entry">
            <primary-entry>
                <entry-name>
                    <xsl:apply-templates />
                </entry-name>
            </primary-entry>
        </xsl:element>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry/p[@outputclass='TOA_Entry_L2']" priority="127">
        <secondary-entry>
            <entry-name>
                <xsl:apply-templates />
            </entry-name>
        </secondary-entry>
    </xsl:template>
    <xsl:template match="topic[@outputclass='TOA_Entries']/body/table[@outputclass='TableGrid0']/tgroup/tbody/row/entry/p[@outputclass='TOA_ID']" priority="126" />
</xsl:transform>

我得到这样的输出:

<!DOCTYPE html PUBLIC "XSLT-compat">
<toa type="toa">
    <title>Table of Authorities – Entries</title>
    <body>
        <toa-section>
            <title>Structures</title>
            <toa-div level="div1">
                <title>Carnivanal Code (26 U.S.C.)</title>
            </toa-div>
            <toa-entry>
                <primary-entry>
                    <entry-name>ch. 61</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-entry>
                <primary-entry>
                    <entry-name>I.R.C. of 1931</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-div level="div1">
                <title>Investment Company Act of 1940</title>
            </toa-div>
            <toa-div level="div2">
                <title>Rules</title>
            </toa-div>
            <toa-entry>
                <primary-entry>
                    <entry-name>202(a)(11)(G)-1</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-div level="div1">
                <title>Securities Act of 1933</title>
            </toa-div>
            <toa-div level="div2">
                <title>Rules</title>
            </toa-div>
            <toa-entry>
                <primary-entry>
                    <entry-name>202(a)(30)-1(a)</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-entry>
                <primary-entry>
                    <entry-name>202(a)(30)-1(a)(1)</entry-name>
                </primary-entry>
            </toa-entry>
        </toa-section>
        <toa-section>
            <title>Cases</title>
            <toa-entry>
                <primary-entry>
                    <entry-name>3M Co. v. Browner</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-div level="div1">
                <title>A</title>
            </toa-div>
            <toa-entry>
                <primary-entry>
                    <entry-name>Abbate v. United States</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-entry>
                <primary-entry>
                    <entry-name>Abel v. United States</entry-name>
                </primary-entry>
            </toa-entry>
            <toa-div level="div1">
                <title>B</title>
            </toa-div>
            <toa-entry>
                <primary-entry>
                    <entry-name>Bacon v. Comm’r</entry-name>
                </primary-entry>
            </toa-entry>
            <secondary-entry>
                <entry-name>September 1, 1982</entry-name>
            </secondary-entry>
            <secondary-entry>
                <entry-name>January 8, 1984</entry-name>
            </secondary-entry>
            <toa-entry>
                <primary-entry>
                    <entry-name>Compaq Computer Corp. v. Comm’r</entry-name>
                </primary-entry>
            </toa-entry>
        </toa-section>
    </body>
</toa>

需要的输出格式如下两种格式

toa-div内需要toa-entry以及toa-entry需要primary-entrysecondary-entry元素,如下所示输出格式。

如果直接TOA_title出现在输入XML中,则需要toa-divtitle自我关闭格式,如下所示输出格式。

<!DOCTYPE html PUBLIC "XSLT-compat">
<toa type="toa">
    <title outputclass="TOA_E">Table of Authorities – Entries</title>
    <body>
        <toa-section>
            <title>Structures</title>
            <toa-div level="div1">
                <title>Carnivanal Code (26 U.S.C.)</title>
                <toa-entry id="S_IRC_ch61">
                    <primary-entry>
                        <entry-name>ch. 61</entry-name>
                    </primary-entry>
                </toa-entry>
                <toa-entry id="S_IRC_IRC1931">
                    <primary-entry>
                        <entry-name>I.R.C. of 1931</entry-name>
                    </primary-entry>
                </toa-entry>
            </toa-div>
            <toa-div level="div1">
                <title>Investment Company Act of 1940</title>
                <toa-div level="div2">
                    <title>Rules</title>
                    <toa-entry>
                        <primary-entry id="S_ICA_R_ICA1940">
                            <entry-name>202(a)(11)(G)-1</entry-name>
                        </primary-entry>
                    </toa-entry>
                </toa-div>
            </toa-div>
            <toa-div level="div1">
                <title>Securities Act of 1933</title>
                <toa-div level="div2">
                    <title>Rules</title>
                    <toa-entry>
                        <primary-entry id="S_SA_R_SA1933">
                            <entry-name>202(a)(30)-1(a)</entry-name>
                        </primary-entry>
                    </toa-entry>
                    <toa-entry>
                        <primary-entry id="S_SA_R_SA1933_1">
                            <entry-name>202(a)(30)-1(a)(1)</entry-name>
                        </primary-entry>
                    </toa-entry>
                </toa-div>
            </toa-div>
        </toa-section>
        <toa-section>
            <title>Cases</title>
            <toa-div level="div1">
                <title />
                <toa-entry>
                    <primary-entry id=">C_3MCoVBrowner">
                        <entry-name>3M Co. v. Browner</entry-name>
                    </primary-entry>
                </toa-entry>
            </toa-div>
            <toa-div level="div1">
                <title>A</title>
                <toa-entry>
                    <primary-entry id="C_AbbateVUS">
                        <entry-name>Abbate v. United States</entry-name>
                    </primary-entry>
                </toa-entry>
                <toa-entry>
                    <primary-entry id="C_AbelVUS">
                        <entry-name>Abel v. United States</entry-name>
                    </primary-entry>
                </toa-entry>
            </toa-div>
            <toa-div level="div1">
                <title>B</title>
                <toa-entry>
                    <primary-entry>
                        <entry-name>Bacon v. Comm’r</entry-name>
                    </primary-entry>
                </toa-entry>
                <secondary-entry id="C_BaconVCommr1">
                    <entry-name>September 1, 1982</entry-name>
                </secondary-entry>
                <secondary-entry id="C_BaconVCommr2">
                    <entry-name>January 8, 1984</entry-name>
                </secondary-entry>
                <toa-entry>
                    <primary-entry id="C_CompaqCompVCommr">
                        <entry-name>Compaq Computer Corp. v. Comm’r</entry-name>
                    </primary-entry>
                </toa-entry>
            </toa-div>
        </toa-section>
    </body>
</toa>

请建议我。 在此先感谢!

0 个答案:

没有答案