简体   繁体   English

合并2个具有相同名称XSLT的元素

[英]Merge 2 elements with the same name XSLT

I can't find a working solution to my problem. 我找不到解决我问题的可行解决方案。 I need to merge two elements with the same name into one when they are in specific position in the document: tags hi when between them stays lb break='no'. 当它们在文档中的特定位置时,我需要将两个具有相同名称的元素合并为一个:当标签在它们之间停留lb break ='no'时,标签为hi。 How can I do that? 我怎样才能做到这一点? The xml code is: xml代码是:

<p>
 <lb n="28"/> Lorem ipsum dolor sit
 <lb n="29"/> amet, consectetur, <hi rend="u">adi</hi>
 <lb n="30" break="no"/><hi rend="u">pisci</hi> elit...
</p>

I transform it into a floating text without line breakes and as there is a hyphen there I need to merge the two elements into one. 我将其转换为没有换行符的浮动文本,并且由于存在连字符,因此需要将两个元素合并为一个。 Now I get always a blank between them. 现在,它们之间总是空白。

<p>Lorem ipsum dolor sit amet, consectetur <span class="u">adipisici</span> elit...</p>

Thanks a lot! 非常感谢!

Assuming an XSLT 2.0 processor like Saxon 9 or XmlPrime you can use 假设您可以使用Saxon 9或XmlPrime之类的XSLT 2.0处理器

<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:template match="p">
  <xsl:copy>
    <xsl:for-each-group select="node() except text()[not(normalize-space())]"
      group-adjacent="self::hi or self::lb[@break = 'no']">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <span class="{current-group()[self::hi][1]/@rend}">
            <xsl:apply-templates select="current-group()/node()"/>
          </span>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </xsl:copy>
</xsl:template>

</xsl:stylesheet>

to transform 转变

<p>
 <lb n="28"/> Lorem ipsum dolor sit
 <lb n="29"/> amet, consectetur, <hi rend="u">adi</hi>
 <lb n="30" break="no"/><hi rend="u">pisci</hi> elit...
</p>

into

<p> Lorem ipsum dolor sit
  amet, consectetur, <span class="u">adipisci</span> elit...
</p>

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

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