English 中文(简体)
如何使用xslt合并元素?
原标题:how to merge element using xslt?
  • 时间:2009-10-09 09:25:51
  •  标签:

我有一款参考类型和内容。

投入文件:

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

我的字母:

<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> 部分时间,如所需产出档案?

请建议任何人。

By, Antny.

问题回答

这是一种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
  ]"
/>

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

这里是我的方法,它使用重复性电话,使各种要素与同类内容相匹配。

它首先与第一个精干元素相对应,但又一次称之为一个模版,对同类元素加以匹配。 其次,它重复了与目前相对应的下一个新要素相匹配的过程。

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

但是,如果目前失败的话,那就等于混血分子之间的白色空间。





相关问题
热门标签