如何使用xslt合并元素?

2024-03-09

我有一个带有元素的段落参考类型。

Example

输入文件:

<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>元素单次像所需的输出文件一样?

请任何人建议..

经过, 安尼.


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

如果有人有更好的主意,请告诉我。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用xslt合并元素? 的相关文章

随机推荐

  • 条件线程中断

    在我正在编写的一些代码中 有几个地方我做了这样的事情 public MyThread extends Thread boolean finished false BlockingQueue
  • 在多少种语言中,Null 不等于任何非 Null 的东西?

    在多少种语言中 Null 不等于任何非 Null 的东西 SQL 作为一种逻辑语言 就是这样 因为 null 意味着未知 未定义 然而 在编程语言 例如 C 或 C 中 空指针 引用是具有特定含义的特定值 什么都没有 两个无是等价的 但两个
  • 为什么 console.log 的行为是这样的?

    在 Node js 解释器上 console log A newline character is written like n output is A newline character is written like n 但是当你简单地
  • 如何在 Windows 上使用 ImageMagick 命令行?

    我的目标是确定我拥有的 jpeg 图像的压缩参数 据我了解这个答案 https stackoverflow com questions 2024947 is it possible to tell the quality level of
  • Xamarin.iOS 中带有填充的 UILabel?

    我正在尝试创建一个UILabel在我的 Xamarin iOS 应用程序中使用填充 原生 Objective C 应用程序中最流行的解决方案是重写drawTextInRect void drawTextInRect CGRect rect
  • Emacs:临时缓冲区的弹出底部窗口

    我想要一个用于临时缓冲区的弹出底部窗口 例如汇编 竣工数量等 即使根窗口水平分割 它也应该垂直分割整个框架 例如 M x 编译前 After 我绝对满意ecb 编译窗口 http ecb sourceforge net docs ecb c
  • while 和 for 哪个循环更快?

    您可以使用 for 和 while 循环获得相同的输出 While i 0 while i lt 10 print i n i For for i 0 i lt 10 i print i n 但哪一个更快呢 这显然取决于特定语言的解释器 编
  • IEnumerator 实现

    我有一个这个代码 public class SomeClass
  • 消除网格间隙

    我有一个 div 其元素对齐为一行 这是它的 css 类 myRow display grid grid template columns 0 1fr 0 1fr 2fr 3fr 2fr grid column gap 10px grid
  • 如何在 OpenSSL 中获取 SSL 证书

    因此 我一直在寻找如何在我正在开发的 C 应用程序中的 OpenSSL 中验证服务器的证书 我终于得到了提示 但是 我仍然缺少一些步骤 所以我发现OpenSSL有一个名为s client的ssl客户端应用程序 当我使用以下命令时 echo
  • 自定义qt项目时使用条件

    再会 我有一个 qt 项目 我想使用 pro 文件条件对其进行自定义 值得注意的是 我想使用一个 pro 文件来获取多个输出 如下所示 DEFINES APP1 0 APP2 1 DEFINES TYPE APP1 if TYPE APP1
  • AG-Grid 大数据集渲染时间(慢)

    我有一个网格 其中包含大量但合理的数据 大约 12 000 个单元格 340 列和 34 行 我知道这看起来像是一个横向表格 但对于我们的应用程序来说 它更可能有大量的列和更少的行 当数据约为 2300 个单元格 68 列和 34 行 时
  • 如何访问pgadmin数据库设计器?

    我正在运行 pgadmin 1 18 在选项窗格中 我可以设置数据库设计器的字体 但我发现绝对无法访问此数据库设计器窗口 没有图标或菜单或任何 这是 pgadmin 1 18 下的可用功能吗 应该启用它吗 图形查询生成器 Source ht
  • Accurev 中的 diff 与 basic 和 backing 之间有什么区别

    Accurev 中与基础的差异和与支持的差异有什么区别 我从您在本论坛中的其他帖子中假设 这里的上下文将是在您的工作区中根据 支持 或 基础 对文件进行比较 与基础进行比较将在进行更改之前将您工作区中当前拥有的文件与您开始使用的版本进行比较
  • Get-ADUser 错误:枚举上下文无效

    我前几天发布了这个问题从分组对象中提取电子邮件 https stackoverflow com questions 30856287 extract e mail from grouped objects 30856711 noredire
  • AFNetworking 启用 GZIP

    我在 AFNetworking 网站上查看支持 GZIP 压缩 服务器响应的 Gzip 解压缩已内置于 AFNetworking 中 因为 NSURLConnection 将使用 Content Encoding gzip HTTP 标头自
  • ActionMailer和开发模式,可以写入文件什么的吗?

    我想在本地测试我的注册过程 开发模式 如何测试电子邮件的发送和呈现方式等 我不是指单元测试或集成测试 而是指在开发我的应用程序并进入注册页面等时 我希望它发送电子邮件 但发送到不使用 smtp 的文件 这可能吗 我有什么选择 这是可配置的c
  • 有没有办法让 Javascript 在 DOMPDF 生成的 PDF 中工作?

    我目前正在测试 DOMPDF 并让它非常适合我的目的 包括 CSS 样式 显示从 mysql 数据库获取的内容等 现在我尝试使用一些Javascript 但它不起作用 我使用了一个非常简单的脚本进行测试 页面上某处的 HTML div st
  • 在 WKUIDelegate SwiftUI 上实现 Javascript 警报并确认?

    由于我是 Swift 新手 我不确定如何为 Swift 编写一个函数 以便从 Web 应用程序进行交互式 Javascript 警报和确认 我正在使用 SwiftUI 创建一个 Web 应用程序 需要为我的 Swift Web 应用程序实现
  • 如何使用xslt合并元素?

    我有一个带有元素的段落参考类型 Example 输入文件