是否可能在具有不同输出类别的para之间使用根元素

时间:2017-07-13 08:55:18

标签: xml xslt

如果输入中有根元素,我们可以使用outputclasses

生成根元素

我的输入Xml

<?xml version="1.0" encoding="UTF-8"?>
<topic outputclass="F_M" id="1234">
   <title outputclass="F_M_T">Officers</title>
   <body class="- topic/body ">
      <p outputclass="FM_B_S">Stregth• <br/>Caution• <br/>Artist • Dress</p>
      <p outputclass="FM_B_E">Edition</p>
      <p outputclass="FM_V">1</p>
      <image href="base/images/lan.png" outputclass="I_R"/>
      <p outputclass="FM_LH">Starter titles</p>
      <p outputclass="FM_LT">Commerce</p>
      <p outputclass="FM_L_B">Cartism</p>
      <p outputclass="FM_LT">Cat</p>
      <p outputclass="FM_L_B">Comunication</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_LT">TAMKEUPTCY BAN</p>
      <p outputclass="FM_L_B">Tankgruptgy Vesk</p>
      <p outputclass="FM_L_B">Resource Tankruptcy Book</p>
      <p outputclass="FM_O_I">Order now at www.ram.dev/ruds</p>
      <p outputclass="FM_O_I">Or call (900) 371-5645 Tue.–Wed., 5 a.m.–8 p.m.</p>
      <p outputclass="FM_P_A">Ram Institute</p>
      <p outputclass="FM_P_A">2255 Avenue of the Casinos</p>
      <p outputclass="FM_P_A">Melon Park, MP 20145</p>
      <p outputclass="FM_PC">States that the may undertake a review of the definition MLP5-Y.</p>
      <p outputclass="FM_B_T">States that the may undertake a review</p>
      <p outputclass="FM_B_S">Resource Tankruptcy Book<br/>• Resource Tankruptcy Book • States • Tankgruptgy<br/>• Definition • Melon Card</p>
      <p outputclass="FM_B_E">Book Edition</p>
      <p outputclass="FM_V">1</p>
      <p outputclass="FM_A">Ramesh G. Kumar, Sr.</p>
      <p outputclass="FM_I">Pre release number No. 50<br/>September 2016<br/>#256799</p>
      <p outputclass="FM_N_B">Ram Institute<br/>Melon Park City<br/>#25697</p>
      <p>
         <b>
            <br/>
         </b>
      </p>
      <p outputclass="FM_D">1Y The Act also states that the may undertake a review of the definition of the term.</p>
      <p outputclass="FM_Q">QUESTIONS ABOUT THIS BOOK?</p>
      <p outputclass="FM_Q">States that the may undertake a review of the definition of the term (900) 450-4RAM.</p>
      <p outputclass="FM_QT">States that the may undertake a review of the definition of the term (766) 199-6302 or email: brendoprecautions@ram.dev.</p>
      <p outputclass="FM_QT">States that the may undertake a review of the definition of the term at: redial@ram.dev.</p>
      <p outputclass="FM_QT">States that the may undertake a review of the definition of the term visit <xref format="html" href="YYYhttp://www.ram.dev" outputclass="regular">www.ram.dev</xref>.</p>
      <p outputclass="FM_LE">Common Prediction: Melln Gavagh</p>
      <p outputclass="FM_C">Ramright © 2014, 2015 by ram Institute. </p>
      <p outputclass="FM_C">Undertake a review of the definition. </p>
      <p outputclass="FM_C">States that the may undertake a review of the definition of the term ram Institute.</p>
      <p outputclass="FM_I_L">States that the may undertake a review of the definition of the term Number: 1921042468</p>
      <p outputclass="FM_I_L">ISBN: 969-2-3133-2664-8</p>
   </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()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="topic[@outputclass='F_M']">
  <xsl:variable name="createdid" select="@id"/>
      <book create-date="June 2017" bid="{$createdid}">
        <xsl:apply-templates/>
      </book>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_B_S']">
    <subtitle>
      <xsl:apply-templates/>
    </subtitle>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_B_E']"/>

  <xsl:template match="p[@outputclass='FM_V']"/>

  <xsl:template match="p[@outputclass='LH']">
    <para>
      <xsl:apply-templates/>
    </para>
  </xsl:template>

  <xsl:key name="kFollowing2" match="p[@outputclass='FM_L_B'][preceding-sibling::*[1][self::p[@outputclass='FM_L_B']]]" use="generate-id(preceding-sibling::p[@outputclass='FM_L_B'][not(preceding-sibling::*[1][self::p[@outputclass='FM_L_B']])][1])"/>

  <xsl:template match="p[@outputclass='FM_L_B'][not(preceding-sibling::*[1][self::p[@outputclass='FM_L_B']])]">
    <publist>
      <xsl:call-template name="FM_L_B1"/>
      <xsl:apply-templates mode="copy" select="key('kFollowing2', generate-id())"/>
    </publist>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_L_B'][preceding-sibling::*[1][self::p[@outputclass='FM_L_B']]]"/>

  <xsl:template match="p[@outputclass='FM_L_B']" mode="copy" name="FM_L_B1">
    <publication><pubtitle><xsl:apply-templates/></pubtitle></publication>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_LT']">
    <para align="left"><content-style font-style="bold">
      <xsl:apply-templates/>
    </content-style></para>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_O_I']">
    <para align="center">
      <xsl:apply-templates/>
    </para>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_P_A']">
    <para align="center">
      <xsl:apply-templates/>
    </para>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_PC']">
    <para align="center">
      <xsl:apply-templates/>
    </para>
  </xsl:template>

  <xsl:template match="p[@outputclass='FM_D']">
    <legalnotice>
      <para><content-style font-style="italic">
      <xsl:apply-templates/>
      </content-style></para>
    </legalnotice>
  </xsl:template>

  <xsl:template match="image[@outputclass='I_R']">
    <figure>
      <xsl:attribute name="id" select="concat('fig_', format-number(count(preceding::image[@outputclass='Image_Ref'])+1,'000'))"/>
      <xsl:element name="graphic">
        <xsl:attribute name="href">
          <xsl:value-of select="substring-after(@href, 'base/images/')"/>
        </xsl:attribute>
        <xsl:attribute name="width">
            <xsl:value-of select="'100%'"/>
          </xsl:attribute>
      </xsl:element>
    </figure>
  </xsl:template>

  <xsl:template match="xref[@outputclass='regular']">
    <xsl:element name="uri">
      <xsl:attribute name="href">
        <xsl:value-of select="@href"/>
        </xsl:attribute>
        <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="title">
    <title>
      <xsl:apply-templates/>
    </title>
  </xsl:template>

  <xsl:template match="body">
    <xsl:apply-templates/>
  </xsl:template>

</xsl:transform>

我的输出为

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<book create-date="June 2017" bid="1234">

   <title>Officers</title>


   <subtitle>Stregth• <br>Caution• <br>Artist • Dress
   </subtitle>



   <figure id="fig_001">
      <graphic href="lan.png" width="100%"></graphic>
   </figure>

   <p outputclass="FM_LH">Starter titles</p>

   <para align="left">
      <content-style font-style="bold">Commerce</content-style>
   </para>

   <publist>
      <publication>
         <pubtitle>Cartism</pubtitle>
      </publication>
   </publist>

   <para align="left">
      <content-style font-style="bold">Cat</content-style>
   </para>

   <publist>
      <publication>
         <pubtitle>Comunication</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
   </publist>








   <para align="left">
      <content-style font-style="bold">TAMKEUPTCY BAN</content-style>
   </para>

   <publist>
      <publication>
         <pubtitle>Tankgruptgy Vesk</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
   </publist>


   <para align="center">Order now at www.ram.dev/ruds</para>

   <para align="center">Or call (900) 371-5645 Tue.–Wed., 5 a.m.–8 p.m.</para>

   <para align="center">Ram Institute</para>

   <para align="center">2255 Avenue of the Casinos</para>

   <para align="center">Melon Park, MP 20145</para>

   <para align="center">States that the may undertake a review of the definition MLP5-Y.</para>

   <p outputclass="FM_B_T">States that the may undertake a review</p>

   <subtitle>Resource Tankruptcy Book<br>• Resource Tankruptcy Book • States • Tankgruptgy<br>• Definition • Melon Card
   </subtitle>



   <p outputclass="FM_A">Ramesh G. Kumar, Sr.</p>

   <p outputclass="FM_I">Pre release number No. 50<br>September 2016<br>#256799
   </p>

   <p outputclass="FM_N_B">Ram Institute<br>Melon Park City<br>#25697
   </p>

   <p>
      <b>
         <br>
         </b>

   </p>

   <legalnotice>
      <para>
         <content-style font-style="italic">1Y The Act also states that the may undertake a review of the definition of the term.</content-style>
      </para>
   </legalnotice>

   <p outputclass="FM_Q">QUESTIONS ABOUT THIS BOOK?</p>

   <p outputclass="FM_Q">States that the may undertake a review of the definition of the term (900) 450-4RAM.</p>

   <p outputclass="FM_QT">States that the may undertake a review of the definition of the term (766) 199-6302
      or email: brendoprecautions@ram.dev.
   </p>

   <p outputclass="FM_QT">States that the may undertake a review of the definition of the term at: redial@ram.dev.</p>

   <p outputclass="FM_QT">States that the may undertake a review of the definition of the term visit 
      <uri href="YYYhttp://www.ram.dev">www.ram.dev</uri>.
   </p>

   <p outputclass="FM_LE">Common Prediction: Melln Gavagh</p>

   <p outputclass="FM_C">Ramright © 2014, 2015 by ram Institute. </p>

   <p outputclass="FM_C">Undertake a review of the definition. </p>

   <p outputclass="FM_C">States that the may undertake a review of the definition of the term ram Institute.</p>

   <p outputclass="FM_I_L">States that the may undertake a review of the definition of the term Number: 1921042468</p>

   <p outputclass="FM_I_L">ISBN: 969-2-3133-2664-8</p>


</book>

但是我想把它作为&#39; frontmatter&#39;标记为根元素和&#39; seriesinfo&#39;和&#39; pubhistory&#39;对于某些元素以及在此之后的星期三,上​​午5点到晚上8点。 para text para作为地址格式,如下所示:

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<book create-date="June 2017" bid="1234">

   <title>Officers</title>


   <subtitle>Stregth• <br/>Caution• <br/>Artist • Dress <content-style />Volume 1   </subtitle>

   <frontmatter>

   <figure id="fig_001">
      <graphic href="lan.png" width="100%"></graphic>
   </figure>
   <seriesinfo>
<pubhistory>
   <title>Starter titles</title>

   <para align="left">
      <content-style font-style="bold">Commerce</content-style>
   </para>

   <publist>
      <publication>
         <pubtitle>Cartism</pubtitle>
      </publication>
   </publist>

   <para align="left">
      <content-style font-style="bold">Cat</content-style>
   </para>

   <publist>
      <publication>
         <pubtitle>Comunication</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
   </publist>








   <para align="left">
      <content-style font-style="bold">TAMKEUPTCY BAN</content-style>
   </para>

   <publist>
      <publication>
         <pubtitle>Tankgruptgy Vesk</pubtitle>
      </publication>
      <publication>
         <pubtitle>Resource Tankruptcy Book</pubtitle>
      </publication>
   </publist>
   </pubhistory>

   <para align="center">Order now at www.ram.dev/ruds</para>

   <para align="center">Or call (900) 371-5645 Tue.–Wed., 5 a.m.–8 p.m.</para>

   <para align="center"><address><orgname>Ram Institute</orgname><street>2255 Avenue of the Casinos</street><city>Melon Park</city>, <state>MP</state><zipcode>20145</zipcode></address></para>

   <para align="center">States that the may undertake a review of the definition MLP5-Y.</para>
   </seriesinfo>
<bookinfo>
<title>States that the may undertake a review</title>
<otherinfo>
<para align="center">Resource Tankruptcy Book</para>
<para align="center">&#x2022; Resource Tankruptcy Book • States • Tankgruptgy<br>• Definition • Melon Card</para>
</otherinfo>
<authors>
<author><name><firstname>Ramesh </firstname><middlename>G. </middlename><surname>Kumar</surname>, <honorific>Sr.</honorific></name></author>
<printinfo differentiation="former">
<para align="center">Pre release number No. 50<br>September 2016<br>#256799
   </para>
</printinfo>
<publisherinfo>
<para align="center">Ram Institute<br>Melon Park City<br>#25697</para>
</publisherinfo>
   <legalnotice>
      <para>
         <content-style font-style="italic">1Y The Act also states that the may undertake a review of the definition of the term.</content-style>
      </para>
   </legalnotice>
<contactinfo>
<title>QUESTIONS ABOUT THIS BOOK?</title>
<para>States that the may undertake a review of the definition of the term <address><phone>(900) 450-4RAM</phone></address>.</para>

   <para>States that the may undertake a review of the definition of <address>the term <phone>(766) 199-6302</phone>
      or email: <email>brendoprecautions@ram.dev</email>.
   </para>

   <para>States that the may undertake a review of the definition of the term <address>at: <email>redial@ram.dev</email></address>.</para>

   <para>States that the may undertake a review of the definition of the term visit 
      <address><uri href="YYYhttp://www.ram.dev">www.ram.dev</uri></address>.
   </para>

   <copyright>
<para align="left">Common Prediction: Melln Gavagh</para>
<para align="left">Ramright © 2014, 2015 by ram Institute.  Undertake a review of the definition.  States that the may undertake a review of the definition of the term ram Institute.</para>
</copyright>
<catalogrecord><catalog><address />
<para align="center">States that the may undertake a review of the definition of the term Number: 1921042468</para>
</catalog></catalogrecord>
<isbn>ISBN: 969-2-3133-2664-8</isbn>
</bookinfo>
</frontmatter>
<body />
</book>

请帮助我。

先谢谢

1 个答案:

答案 0 :(得分:0)

我可以建议你改变一下你的方法。

例如,您可以使用一个主模板,从中调用带参数的其他模板。

为了做到这一点,请使用带有params的call-template而不是apply-templates。

这种结构可以帮助您形成更清晰,更自定义的转换。

例如,在pubhistory中,您将为某些元素调用所需的模板,然后在“周三,早上5点到晚上8点”之后调用。相应地,可以按所需顺序填写:

  <xsl:template match="body">
       <xsl:call-template name="title"/>
       <xsl:call-template name="subtitle"/>
    <frontmatter>
      <seriesinfo>
        <pubhistory>
          <xsl:call-template name="FM_O_I">
            <xsl:with-param name="FM_O_I" select="p[@outputclass='FM_O_I']"/>
          </xsl:call-template>
          .
          .
          .
          <xsl:call-template name="FM_LT">
            <xsl:with-param name="FM_LT" select="p[@outputclass='FM_LT']"/>
          </xsl:call-template>
        </pubhistory>
      </seriesinfo>
      <xsl:call-template name="OtherTemplate">
         <xsl:with-param name="OtherTemplate" select="someXpath"/>
       </xsl:call-template>
    </frontmatter>
  </xsl:template>

因此,有必要以下列方式重写使用过的tempaltes:

    <xsl:template name="FM_LT">
    <xsl:param name="FM_LT"/>
    <para align="left">
      <content-style font-style="bold">
        <xsl:apply-templates select="$FM_LT"/>
      </content-style>
    </para>
  </xsl:template>
  <xsl:template name="FM_O_I">
    <xsl:param name="FM_O_I"/>
    <para align="center">
      <xsl:apply-templates select="$FM_O_I"/>
    </para>
  </xsl:template>

我希望这种方法能够满足您的要求 我附上了以下转型的主干:

<?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()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template name="FM_LT">
    <xsl:param name="FM_LT"/>
    <para align="left">
      <content-style font-style="bold">
        <xsl:apply-templates select="$FM_LT"/>
      </content-style>
    </para>
  </xsl:template>
  <xsl:template name="FM_O_I">
    <xsl:param name="FM_O_I"/>
    <para align="center">
      <xsl:apply-templates select="$FM_O_I"/>
    </para>
  </xsl:template>
  <xsl:template name="FM_PC">
    <xsl:param name="FM_PC"/>
    <para align="center">
      <xsl:apply-templates select="$FM_PC"/>
    </para>
  </xsl:template>
  <xsl:template name="FM_D">
    <xsl:param name="FM_D"/>
    <legalnotice>
      <para>
        <content-style font-style="italic">
          <xsl:apply-templates select="$FM_D"/>
        </content-style>
      </para>
    </legalnotice>
  </xsl:template>
  <xsl:template match="body">
    <frontmatter>
      <seriesinfo>
        <pubhistory>
          <xsl:call-template name="FM_O_I">
            <xsl:with-param name="FM_O_I" select="p[@outputclass='FM_O_I']"/>
          </xsl:call-template>
          <xsl:call-template name="FM_LT">
            <xsl:with-param name="FM_LT" select="p[@outputclass='FM_LT']"/>
          </xsl:call-template>
        </pubhistory>
        <xsl:call-template name="FM_PC">
          <xsl:with-param name="FM_PC" select="p[@outputclass='FM_PC']"/>
        </xsl:call-template>
      </seriesinfo>
      <xsl:call-template name="FM_D">
        <xsl:with-param name="FM_D" select="p[@outputclass='FM_D']"/>
      </xsl:call-template>
    </frontmatter>
  </xsl:template>
</xsl:transform>