开发者

How to split a string with <br> in xml using xslt-1.0

开发者 https://www.devze.com 2023-03-22 16:51 出处:网络
This is part of the HTML I would like to transform using XSLT <tr> <td>ELE1600A</td> <td>CIRCUITS ELECTRIQUES<br>Chahe Nerguizian</td>

This is part of the HTML I would like to transform using XSLT

<tr>
<td>ELE1600A  </td>
<td>CIRCUITS ELECTRIQUES                         <br>Chahe Nerguizian                                  </td>
<td><center>01&nbsp;</center></td>
<td><center>01&nbsp;</center></td>
<td><center>03</center></td>
</tr>

I would like to split the following

<td>CIRCUITS ELECTRIQUES                         <br>Chahe Nerguizian                                  </td> 

into :

  1. CIRCUITS ELECTRIQUES
  2. Chahe Nerguizian

I've tried using

<xsl:valuf-of select="substring-before(td[2],'&#xA;')"/>
<xsl:valuf-of s开发者_JS百科elect="substring-after(td[2],'&#xA;')"/>

but it does not return anything to me in both php's and eclipse's XSLT Processor. Any tough on how could I achieve this ?

Thanks in advance.


As mentioned in the comments, you have not got XHTML here, and therefore not XML, and so XSLT cannot be used on it. However.... IF it was amended to be XML, you could do something with it.

Imagine this was the starting document, which is well-formed:

<tr>
    <td>ELE1600A </td>
    <td>CIRCUITS ELECTRIQUES<br />Chahe Nerguizian 
    </td>
    <td>
        <center>01 </center>
    </td>
    <td>
        <center>01 </center>
    </td>
    <td>
        <center>03</center>
    </td>
</tr>

You can then make use of the identity transform, with extra matching templates to handle matching the td element which has br elements as children.

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

   <xsl:output method="html" />

   <xsl:template match="td[br]">
      <xsl:copy>
         <ol>
            <xsl:apply-templates />
         </ol>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="td[br]/node()">
      <li>
         <xsl:call-template name="IdentityTransform" />
      </li>
   </xsl:template>

   <xsl:template match="td[br]/br">
      <!-- Ignore tag -->
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:call-template name="IdentityTransform" />
   </xsl:template>

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

</xsl:stylesheet>

When this is applied to the input XML, the following is generated:

<tr>
    <td>ELE1600A </td>
    <td>
        <ol>
            <li>CIRCUITS ELECTRIQUES </li>
            <li>Chahe Nerguizian </li>
        </ol>
    </td>
    <td>
        <center>01 </center>
    </td>
    <td>
        <center>01 </center>
    </td>
    <td>
        <center>03</center>
    </td>
</tr>
0

精彩评论

暂无评论...
验证码 换一张
取 消