简体   繁体   English

如何拆分字符串<br>在 xml 中使用 xslt-1.0

[英]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这是 HTML 的一部分,我想使用 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.但它在 php 和 eclipse 的 XSLT 处理器中都没有返回任何东西给我。 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.正如评论中提到的,你这里没有 XHTML,因此没有 XML,所以不能在上面使用 XSLT。 However.... IF it was amended to be XML, you could do something with it.然而....如果它被修改为 XML,你可以用它做点什么。

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.然后,您可以使用身份转换,并使用额外的匹配模板来处理匹配以br元素为子元素的td元素。

<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:当这应用于输入 XML 时,将生成以下内容:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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