将连续的后代节点合并为一个

2023-12-24

XML:

<t>
  <ScreenSize>
    <Width>1440</Width>
    <Height>900</Height>
  </ScreenSize>
  <ConfigurationHotSpots>
    <Rectangle>
      <Location>
        <X>0</X>
        <Y>0</Y>
      </Location>
      <Size>
        <Width>50</Width>
        <Height>50</Height>
      </Size>
      <X>0</X>
      <Y>0</Y>
      <Width>50</Width>
      <Height>50</Height>
    </Rectangle>
  </ConfigurationHotSpots>
</t>

所需的输出 XML:

<t>
  <ScreenSizeWidth>1440</ScreenSizeWidth>
  <ScreenSizeWidth>900</ScreenSizeWidth>
  <ConfigurationHotSpotsRectangleLocationX>0</ConfigurationHotSpotsRectangleLocationX>
  <ConfigurationHotSpotsRectangleLocationY>0</ConfigurationHotSpotsRectangleLocationY>
  <ConfigurationHotSpotsRectangleSizeWidth>50</ConfigurationHotSpotsRectangleSizeWidth>
  <ConfigurationHotSpotsRectangleSizeHeight>50</ConfigurationHotSpotsRectangleSizeHeight>
  <ConfigurationHotSpotsRectangleX>0</ConfigurationHotSpotsRectangleX>
  <ConfigurationHotSpotsRectangleY>0</ConfigurationHotSpotsRectangleY>
  <ConfigurationHotSpotsRectangleWidth>50</ConfigurationHotSpotsRectangleWidth>
  <ConfigurationHotSpotsRectangleHeight>50</ConfigurationHotSpotsRectangleHeight>
</t>

Rules:

  • 对于定义的节点集中的每个元素(在本例中<ScreenSize> | <ConfigurationHotSpots>),执行以下操作:处理所有叶子后代(即没有子元素的叶子),以便创建新元素;这个新元素的名称应该是当前节点和无子节点之间的所有元素的串联。
  • 整个文档中这些“块”的数量是可变的,因此没有手动模板(即仅处理<ScreenSize>,仅处理的后代<ConfigurationHotSpots>, etc.)

我目前拥有的:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes" />
  <xsl:strip-space elements="*" />

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="ScreenSize|ConfigurationHotSpots">
    <xsl:apply-templates select="descendant::*[not(*)]" mode="descendants" />
  </xsl:template>

  <xsl:template match="*" mode="descendants">
    <xsl:element name="{concat(name(ancestor::*[not(self::t)]), name())}">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

问题似乎是name(ancestor::*[not(self::t)])部分;它没有做我希望它做的事情(神奇地一个接一个地输出这些元素的名称)。相反,这就是我得到的:

<?xml version="1.0" encoding="UTF-8"?>
<t>
  <ScreenSizeWidth>1440</ScreenSizeWidth>
  <ScreenSizeHeight>900</ScreenSizeHeight>
  <ConfigurationHotSpotsX>0</ConfigurationHotSpotsX>
  <ConfigurationHotSpotsY>0</ConfigurationHotSpotsY>
  <ConfigurationHotSpotsWidth>50</ConfigurationHotSpotsWidth>
  <ConfigurationHotSpotsHeight>50</ConfigurationHotSpotsHeight>
  <ConfigurationHotSpotsX>0</ConfigurationHotSpotsX>
  <ConfigurationHotSpotsY>0</ConfigurationHotSpotsY>
  <ConfigurationHotSpotsWidth>50</ConfigurationHotSpotsWidth>
  <ConfigurationHotSpotsHeight>50</ConfigurationHotSpotsHeight>
</t>

提前致谢!


Doing name(ancestor::*[not(self::t)])不会返回名称列表,而只返回最后一个匹配的名称(或者是第一个?)。

您可以采取稍微不同的方法,与您当前正在做的事情相差不远,不是直接跳到“叶子”元素,而是依次匹配每个级别,而是保持传递的元素名称的连续串联通过参数从一个级别到另一个级别。

试试这个 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="ScreenSize|ConfigurationHotSpots">
        <xsl:apply-templates mode="descendants">
            <xsl:with-param name="name" select="local-name()" />
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="*" mode="descendants">
        <xsl:param name="name" />
        <xsl:apply-templates mode="descendants">
            <xsl:with-param name="name" select="concat($name, local-name())" />
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="*[not(*)]" mode="descendants">
        <xsl:param name="name" />
        <xsl:element name="{concat($name, local-name())}">
            <xsl:value-of select="." />
        </xsl:element>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

当应用于示例 XML 时,输出如下

<t>
   <ScreenSizeWidth>1440</ScreenSizeWidth>
   <ScreenSizeHeight>900</ScreenSizeHeight>
   <ConfigurationHotSpotsRectangleLocationX>0</ConfigurationHotSpotsRectangleLocationX>
   <ConfigurationHotSpotsRectangleLocationY>0</ConfigurationHotSpotsRectangleLocationY>
   <ConfigurationHotSpotsRectangleSizeWidth>50</ConfigurationHotSpotsRectangleSizeWidth>
   <ConfigurationHotSpotsRectangleSizeHeight>50</ConfigurationHotSpotsRectangleSizeHeight>
   <ConfigurationHotSpotsRectangleX>0</ConfigurationHotSpotsRectangleX>
   <ConfigurationHotSpotsRectangleY>0</ConfigurationHotSpotsRectangleY>
   <ConfigurationHotSpotsRectangleWidth>50</ConfigurationHotSpotsRectangleWidth>
   <ConfigurationHotSpotsRectangleHeight>50</ConfigurationHotSpotsRectangleHeight>
</t>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将连续的后代节点合并为一个 的相关文章

随机推荐

  • 多态性(继承)和值类型

    我有很多类型 PixelMeasure PointMeasure CentimeterMeasure等等 表示带有单位的值 我希望他们有 值语义 例如实际上是不可变的 不必担心内存分配 并且 多态性 我可以返回一个类型的对象Measure并
  • 在 Express 中监听 UDP 消息

    我有一个使用 Express 的基本 Node js 服务器 它需要能够处理 TCP 消息以及 UDP 消息 TCP 部分已启动并运行良好 但现在我需要集成一种嗅探 UDP 消息的方法 我尝试使用以下方法将处理程序添加到过滤器堆栈中dgra
  • java中的toArray与stream.toArray有什么性能差异

    我需要将 ids 列表转换为 ids 数组 我可以通过多种方式做到这一点 但不确定应该使用哪一种 Say 1 ids stream toArray Id new 2 ids toArray new Id ids length 哪一种更有效
  • 使用 XmlDocument 保留 xml 格式

    我正在使用 XmlDocument 来处理 xml 如何使用当前格式保存 XmlDocument 当前格式
  • pdb 绕过错误/跳转失败:只能从“行”跟踪事件跳转

    我正在尝试使用 pdb 调试 Python 程序 程序可能是这样的 def main a 1 print b c 2 d 3 显然 print b 是一个拼写错误 应该是 print a 但这并不重要 我可以用文本编辑器修复它 但我想绕过这
  • Linux系统上打印文件的uid

    我正在学习c编程 我正在尝试让我自己的程序类似于ls命令但选项较少 我正在做的是将输入目录 文件名作为参数 然后获取所有目录条目direntstruct 如果是目录 之后我使用 stat 获取文件的所有信息 但是当我使用 write 打印这
  • Node.js:类型错误:对象不是函数

    我有一个奇怪的错误 var http require http var request require request http createServer function request response response writeHe
  • 如何高效替换XML的多个节点?

    我试图一次性替换单个文档的大约 500 个节点 并且我的数据库中有 5000 多个文档 我正在使用的代码与我之前问过的这个问题相关 Link https stackoverflow com questions 51998598 how to
  • EJB3.1 系统异常与 javax.ejb.EJBException

    在提出我的问题之前 先介绍一下 EJB3 1 异常的背景知识 应用程序例外包括 用户定义的已检查或未检查异常 ApplicationException注解 所有已检查的异常 java lang Exception及其子类异常 除了 java
  • 更改 IP 地址后 Riak 节点不再工作

    我使用实例化 Amazon EC2 虚拟 Ubuntu 12 04 服务器作为我的单个 Riak 节点 我已经使用 basho 网站上的指南完成了在实例上设置 Riak 的所有正确阶段here http docs basho com ria
  • 使用 Queue::fake() 测试监听器

    我的 Laravel 5 5 应用程序有一个Product模型 这Product模型有一个dispatchesEvents属性看起来像这样 The event map for the model var array protected di
  • 内存使用量指标标识符 Google Compute Engine

    我已经在我的实例组磁盘中安装了监控代理 我需要根据内存使用情况自动缩放实例 但是当我要在 GCE Web 控制台中配置目标指标时 我错过了内存使用指标标识符 缺少哪个标识符或者如何根据内存使用情况自动缩放我的组 None
  • 反应式编程的优点/缺点

    我不断研究和尝试使用 Reactor 和 RxJava 的响应式编码 我确实明白 与单线程执行相比 反应式编码可以更好地利用 CPU 在基于 Web 的应用程序中 反应式编程与命令式编程之间有具体的比较吗 与非反应式编程相比 使用反应式编程
  • 如何防止 BIDS 自动签出 SSIS 包?

    当编辑文件时 我将 Visual Studio 2005 BIDS 设置为 自动签出 这对于大多数文件类型都适用 但是 当我访问 SSIS 包中的数据流时 会触发签出 而无需我进行任何更改 当我比较这些文件时 后台似乎也没有进行任何更改 这
  • sql server 如何在有多个选项的更新语句中选择值?

    我在 SQL Server 中有一个更新语句 其中可以根据连接分配四个可能的值 SQL 似乎有一种算法可以选择一个值而不是另一个值 但我不确定该算法是如何工作的 举个例子 假设有一个名为 Source 的表 其中有两列 匹配和数据 结构如下
  • MongoDB Mongoose 选择日期范围内的文档

    我有一个名为events 示例文档是这样的 我想选择日期范围内的文档 2019 01 11 to 2019 11 27 我所做的就是这个 但这似乎不起作用 我如何实现这个使用MongoDB Mongoose match createdAt
  • 有没有办法阻止 EF 插入 SQL Server 计算列?

    我不希望 EF 在从数据库检索记录时忽略类上的列 因为我需要能够比较计算列 但我不希望 EF 尝试将值插入到该列中 因为这会抛出异常SQL 异常 因为无法修改计算列 您可以使用数据库生成 https learn microsoft com
  • MockRestServiceServer:如何用主体模拟 POST 调用?

    我试图用以下方式模拟 POST 方法MockRestServiceServer通过以下方式 MockRestServiceServer server bindTo restTemplate build server expect reque
  • R语言中使用getURL()函数返回错误

    我已经开始学习数据科学并且刚接触 R 语言 我正在尝试从下面读取数据使用 getURL 函数和 Rcurl 包的 HTTPS URL 在执行下面的代码时 接收SSL协议问题 R Code 加载库 Rcurl 库 RCurl 指定 Iris
  • 将连续的后代节点合并为一个

    XML