简体   繁体   中英

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

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 select="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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM