简体   繁体   English

使用 xslt 将 xml 元素拆分为两个而不影响其他标签

[英]Split xml elements into two without affecting other tags using xslt

I am converting one xml to other xml.我正在将一个 xml 转换为其他 xml。 I am struggle in splitting this element.我正在努力拆分这个元素。

<div class="cont">      (23)(A) “Component” means a uniquely identifiable <i>homogeneous material</b>, part, piece, <u>assembly</u>, is a necessary or intended element of a consumer product.</div>

Should be应该

<div class="li1">(23)</p>
<div class="li2">(A) “Component” means a uniquely identifiable <i>homogeneous material</i>, part, piece, <u>assembly</u>, is a necessary or intended element of a consumer product.</div>

I tried using below code.我尝试使用下面的代码。 But the <i> and <u> are missing in the output.但是 output 中缺少<i><u>

<xsl:analyze-string select="." regex="^&#x0a;*\s*(\([0-9]+\))(\([A]+\)(.+))">
                    <xsl:matching-substring>
                        <div class="li2"><xsl:copy-of select="regex-group(1)"></xsl:copy-of></div>
                        <div class="li2" par="inline"><xsl:copy-of select="regex-group(2)"></xsl:copy-of></div>
                    </xsl:matching-substring>
                 </xsl:analyze-string>

My output is我的 output 是

<div class="li1">(23)</p>
    <div class="li2">(A) “Component” means a uniquely identifiable homogeneous material, part, piece, assembly, is a necessary or intended element of a consumer product.</div>

Please someone help me to solve this请有人帮我解决这个问题

Perhaps it suffices to split up the text node child with analyze-string and then wrap the second part and any following nodes into the second result div:也许用分析字符串拆分文本节点子节点就足够了,然后将第二部分和任何后续节点包装到第二个结果 div 中:

  <xsl:template match="div[@class = 'cont']">
      <xsl:variable name="this" select="."/>
      <xsl:analyze-string select="node()[1]" regex="^&#x0a;*\s*(\([0-9]+\))">
          <xsl:matching-substring>
              <div class="li2">
                  <xsl:value-of select="regex-group(1)"/>
              </div>
          </xsl:matching-substring>
          <xsl:non-matching-substring>
              <div class="li2" par="inline">
                  <xsl:value-of select="."/>
                  <xsl:apply-templates select="tail($this/node())"/>
              </div>
          </xsl:non-matching-substring>
      </xsl:analyze-string>
  </xsl:template>

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

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