如何使用xslt合并元素? [英] how to merge element using xslt?

查看:24
本文介绍了如何使用xslt合并元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带元素的段落引用类型.

I have an reference type of paragraph with element.

示例

输入文件:

<reference>
<emph type="bold">Antony</emph><emph type="bold">,</emph> <emph type="bold">R.</emph>
<emph type="bold">and</emph> <emph type="bold">Micheal</emph><emph type="bold">,</emph> <emph type="bold">V.</emph>
<emph type="italic">reference title</emph></reference>

现在收到的输出:

<p class="reference"><strong>Antony</strong><strong>,</strong> <strong>R.</strong>
<strong>and</strong> <strong>Micheal</strong><strong>,</emph>
<emph type="bold">V.</strong> <em>reference title></em></p>

所需的输出文件:

<p class="reference"><strong>Antony, R. and Micheal, V.</strong> <em>reference title</em></p>

我的 xslt 脚本:

My xslt scripts:

<xsl:template match="reference">
<p class="reference"><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="emph">
<xsl:if test="@type='bold'">
<strong><xsl:apply-templates/></strong>
</xsl:if>
<xsl:if test="@type='italic'">
<em><xsl:apply-templates/></em>
</xsl:if>
</xsl:template>

需要在 xslt 中更正什么才能像所需的输出文件一样一次性获得 元素?

What needs to be corrected in xslt to get the <strong> element single time like the required output file?

请给任何人建议..

由,蚂蚁.

推荐答案

这是一个 XSLT 1.0 解决方案:

This is an XSLT 1.0 solution:

<xsl:stylesheet 
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" encoding="utf-8" />

  <!-- the identity template copies everything verbatim -->    
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
  </xsl:template>

  <!-- this matches the first <emph> nodes of their kind in a row -->    
  <xsl:template match="emph[not(@type = preceding-sibling::emph[1]/@type)]">
    <xsl:variable name="elementname">
      <xsl:choose>
        <xsl:when test="@type='bold'">strong</xsl:when>
        <xsl:when test="@type='italic'">em</xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:if test="$elementname != ''">
      <!-- the first preceding node with a different type is the group separator -->
      <xsl:variable 
        name="boundary" 
        select="generate-id(preceding-sibling::emph[@type != current()/@type][1])
      " />
      <xsl:element name="{$elementname}">
        <!-- select all <emph> nodes of the row with the same type... -->
        <xsl:variable 
          name="merge" 
          select=". | following-sibling::emph[
            @type = current()/@type
            and 
            generate-id(preceding-sibling::emph[@type != current()/@type][1]) = $boundary
          ]"
        />
        <xsl:apply-templates select="$merge" mode="text" />
      </xsl:element>
    </xsl:if>
  </xsl:template>

  <!-- default: keep <emph> nodes out of the identity template mechanism -->
  <xsl:template match="emph" />

  <!-- <emph> nodes get their special treatment here -->
  <xsl:template match="emph" mode="text">
    <!-- effectively, this copies the text node via the identity template -->
    <xsl:apply-templates />

    <!-- copy the first following node - if it is a text node
         (this is to get interspersed spaces into the output) -->
    <xsl:if test="
      generate-id(following-sibling::node()[1])
      =
      generate-id(following-sibling::text()[1])
    ">
      <xsl:apply-templates select="following-sibling::text()[1]" />
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

结果:

<reference>
  <strong>Antony, R. and Micheal, V.</strong>
  <em>reference title</em>
</reference>

<小时>

我不太满意


I'm not overly happy with

<xsl:variable 
  name="merge" 
  select=". | following-sibling::emph[
    @type = current()/@type
    and 
    generate-id(preceding-sibling::emph[@type != current()/@type][1]) = $boundary
  ]"
/>

如果有人有更好的想法,请告诉我.

if someone has a better idea, please tell me.

这篇关于如何使用xslt合并元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆