XSLT:消失<i> </i>的情况

时间:2014-02-28 15:15:45

标签: html xslt xslt-2.0

感谢您的回复。非常有帮助。下一个问题: 我有一个XML tekst,如:

  <html><body>
    <div class="col2">
      <p>1.  <i>wine</i> is bad</p>
      <p>2.  <i>beer</i> gets you drunk</p>
      <p>3.  food should not be <i>fast</i></p>
    </div>
 </body>
 </html>

我想将其转换为:

<html><body>
    <div class="col2">
    <ol>
       <li><i>wine</i> is bad</li>
       <li><i>beer</i> gets you drunk</li>
       <li>food should not be <i>fast</i></li>
    </ol>
    </div>
  </body></html>

所以我想保留i-tag,转换必须停止。 我怎么能在xslt中做到这一点?

我现在有:(删除了数字,但也删除了i-tag)

  <?xml version="1.0"?>
  <!-- greeting.xsl -->
  <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
<xsl:apply-templates select="@* | node()"/>
  </xsl:template>   

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


  <xsl:template match="div[@class='col2']">
  <div>
<xsl:copy-of select="@*"/>
<ol>
  <xsl:for-each select="p">
  <li>
  <xsl:value-of select="replace( . , '^[\d]*.\s' , '' )"/>
  </li>
  </xsl:for-each>
</ol>
  </div>
  </xsl:template>
  </xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

您需要为text()元素的p个节点子项编写模板:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="yes"/> 

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

<xsl:template match="div[@class='col2']">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <ol>
      <xsl:apply-templates/>
    </ol>
  </xsl:copy>
</xsl:template>

<xsl:template match="div[@class = 'col2']/p[matches(., '^[0-9]+\.')]">
  <li>
    <xsl:apply-templates/>
  </li>
</xsl:template>

<xsl:template match="div[@class = 'col2']/p[matches(., '^[0-9]+\.')]/text()[1]">
  <xsl:value-of select="replace( . , '^[\d]*.\s' , '' )"/>
</xsl:template>

</xsl:stylesheet>

您似乎也想使用分组:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="yes"/>



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


<xsl:template match="div[@class='col2']">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:for-each-group select="node()" 
        group-adjacent="boolean(self::p[matches(., '^[0-9]+\.')] | 
                                self::text()[not(normalize-space())])">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <ol>
            <xsl:apply-templates select="current-group()"/>
          </ol>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

<xsl:template match="div[@class = 'col2']/p[matches(., '^[0-9]+\.')]">
  <li>
    <xsl:apply-templates/>
  </li>
</xsl:template>

<xsl:template match="div[@class = 'col2']/p[matches(., '^[0-9]+\.')]/text()[1]">
  <xsl:value-of select="replace( . , '^[\d]*.\s' , '' )"/>
</xsl:template>

</xsl:stylesheet>

将您的输入转换为

<html>
   <body>

      <div class="col2">
         <ol>

            <li> <i>wine</i> is bad
            </li>

            <li> <i>beer</i> gets you drunk
            </li>

            <li> food should not be <i>fast</i></li>

         </ol>
      </div>

   </body>

</html>