如何使用xslt合并元素?

时间:2009-10-09 09:25:52

标签: xslt xpath

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

实施例

输入文件:

<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脚本:

<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中纠正哪些内容才能使<strong>元素单次获得所需的输出文件?

请建议任何人..

通过, Antny。

2 个答案:

答案 0 :(得分:1)

这是一个XSLT 1.0解决方案:

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

我对

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

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

答案 1 :(得分:0)

这是我的方法,它使用模板的递归调用来匹配具有相同类型的元素。

它首先匹配第一个'emph'元素,并且它们递归地调用匹配相同类型的'emph'元素的模板。接下来,它重复匹配与当前匹配类型不同的下一个'emph'元素的过程。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="html" encoding="utf-8"/>

   <!-- Match root element -->
   <xsl:template match="reference">
      <p class="reference">
         <!-- Match first emph element -->
         <xsl:apply-templates select="emph[1]"/>
      </p>
   </xsl:template>

   <!-- Used to match first occurence of an emph element for any type -->
   <xsl:template match="emph">
      <xsl:variable name="elementname">
         <xsl:if test="@type='bold'">strong</xsl:if>
         <xsl:if test="@type='italic'">em</xsl:if>
      </xsl:variable>
      <xsl:element name="{$elementname}">
         <xsl:apply-templates select="." mode="match">
            <xsl:with-param name="type" select="@type"/>
         </xsl:apply-templates>
      </xsl:element>
      <!-- Find next emph element with a different type -->
      <xsl:apply-templates select="following-sibling::emph[@type!=current()/@type][1]"/>
   </xsl:template>

   <!-- Used to match emph elements of a specific type -->
   <xsl:template match="*" mode="match">
      <xsl:param name="type"/>
      <xsl:if test="@type = $type">
         <xsl:value-of select="."/>
         <xsl:apply-templates select="following-sibling::*[1]" mode="match">
            <xsl:with-param name="type" select="$type"/>
         </xsl:apply-templates>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>

虽然目前失败了,但是它与'emph'元素之间的空格不匹配。