XSLT:递归映射

2024-01-12

我是 XSLT 转换的新手,并且陷入了这种递归映射的困境。

<Element1>
  <Element11/>
  <Element12/>
  <Element13/>
  <Element1>
     <Element11/>
     <Element12/>
     <Element13/>
  </Element1>
</Element1>

将化身为

<Information>
 <Element11/>
 <Element12/>
 <Element13/>
</Information>
<!-- This will be the child Element1 -->
<Metadata>
 <Element11/>
 <Element12/>
 <Element13/>
</Metadata>

我绝对不能使用:

<xsl:template match="/">
            <xsl:for-each select="Element1">
                <Information>
            </xsl:for-each>
    </xsl:template>

这应该可以完成这项工作:

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

  <xsl:output method="xml" indent="yes"/>

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

  <!-- Template handling the top-level 'Element1' -->
  <xsl:template match="Element1">
    <Information>
      <!-- Apply the copy template to all sub-elements except 'Element1' -->
      <xsl:apply-templates select="*[name()!='Element1']"/>
    </Information>
    <!-- Apply the templates to the 'Element1' sub-elements -->
    <xsl:apply-templates select="Element1"/>
  </xsl:template>

  <!-- Template handling the inner 'Element1' -->
  <xsl:template match="Element1/Element1">
    <Metadata>
      <xsl:apply-templates/>
    </Metadata>
  </xsl:template>

</xsl:stylesheet>

正如 Tim 指出的那样,结果不是有效的 XML,因为它有两个根元素。生成额外的root元素以使输出有效 XML 添加此模板:

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

XSLT:递归映射 的相关文章

随机推荐

  • WebRTC 连接在本地网络之外无法工作

    我们对 webrtc 双向视频和音频流进行了以下设置 Mobile Android应用程序使用谷歌网络RTC https webrtc org 实现java包装器 测试了这两个库 implementation org webrtc goog
  • Pandas 将字典列表分解为行

    拥有这个 items name 0 a 2 b 1 a 4 b 3 this 1 a 2 b 1 a 4 b 3 that 但希望将字典对象列表分解为 展平 为实际行 如下所示 a b name 0 2 1 this 1 4 3 this
  • Angular 2 Promise/Observable 链两个事件?

    我想知道是否可观察到的 or promise可用于 Angular 2 中的以下用例 有两个异步上传任务 我想知道如何检测这两项任务都已完成 我的上传任务 实现于promise但它很容易改变为可观察到的如果需要的话 是这样的 myServi
  • 在 macOS 中通过命令行与 Siri 交互

    我在手机和手表上使用 Siri 随时随地创建提醒 当我在办公室时 我不想使用 Siri 打扰安静 因此我通常使用与 提醒 应用程序集成的 Alfred 工作流程 或者直接使用 提醒 应用程序 然而 两者都有一个相当笨拙的界面 如果我可以在命
  • Zbar SDK - 缺少所需的架构 x86_64

    我在最近的 Xcode 5 1 中构建应用程序时遇到了一个问题 编译失败 并出现 架构 x86 64 的未定义符号 错误 我使用有效架构构建我的项目 armv7 armv7s 和 arm64 切换到最新的环境 Xcode 后 我在相同的架构
  • 随机化一个 BigInteger

    I m looking to randomize a BigInteger The intent is to pick a number from 1 to 8180385048 Though from what I noticed the
  • 运行 gulp 任务时如何解决“在 MakeCallback 中使用域属性已被弃用”警告?

    我正在使用带有 gulp 的节点来运行一些构建任务 直到几天前 这一切都还顺利 现在 我假设在升级 更新后 不确定是哪一个 我相信这是节点从 14 4 更新到 14 5 我不断收到此警告 DEP0097 DeprecationWarning
  • 使用 webpack、Threejs 示例和 TypeScript?

    我在将 Threejs 示例 如 EffectComposer 或 Detector 中的内容与 webpack 和 typescript 一起使用时遇到了很多麻烦 先把相关的 d ts文件全部存在并通过安装tsd 我的问题是让 webpa
  • Oracle:“= ANY()”与“IN ()”

    我刚刚在 ORACLE SQL 中偶然发现了一些我很好奇的东西 不确定其他中是否也有 我在这里作为维基询问 因为很难尝试在谷歌中搜索符号 我刚刚发现 当根据一组值检查一个值时 您可以执行以下操作 WHERE x ANY a b c 与通常的
  • 使用 OData 连接服务在 Blazor 客户端应用程序中使用 OData

    创建了 netstandard2 1 blazor Web 程序集项目 将 Odata Connected Service V 0 10 0 添加到同一项目 生成 OData 代理类 从 Razor 页面的 Task OnInitializ
  • 读取 Amazon Kinesis Firehose 流写入 s3 的数据

    我正在将记录写入 Kinesis Firehose 流 该流最终由 Amazon Kinesis Firehose 写入 S3 文件 我的记录对象看起来像 ItemPurchase String personId String itemId
  • document.execCommand 复制命令不起作用或其他解决方案?

    我正在做的是以编程方式从网页中选择所有文本 然后复制它 选择所有适用于execCommand但复制则不然 这是我的代码 ajax url url val type GET success function res result html r
  • Selenium Web 驱动程序等待很长时间

    我可以长时间等待 Selenium Web Driver 吗 尽管我可以像下面这样设置隐式等待命令 但它不会等待我给出的时间 driver manage timeouts implicitlyWait 5 TimeUnit MINUTES
  • C++/Win32:如何等待挂起的删除完成

    Solved 可行的解决方案 履行机构的答复 https stackoverflow com questions 3764072 c win32 how to wait for a pending delete to complete 37
  • 在 Powershell Cmdlet 中使用 Entity Framework Core?

    是否可以构建一个包含实体框架 Core 或 EF6 的 PowerShell cmdlet 以访问 SQL Server 数据库 我多年来一直在用 C 编写 cmdlet 但在过去的 coupla 日子里 由于似乎是程序集版本冲突 我在尝试
  • 在 C++ 中调用 std::sort 时使用 std::greater 的语法

    推荐的方式 例如 按降序对向量进行排序 https stackoverflow com questions 9025084 sorting a vector in descending order 对容器进行反向排序似乎是 std sort
  • Python,规则网格上的邻居

    假设我有一组 2D 坐标 表示 2D 规则网格的单元中心 我想为网格中的每个单元格找到每个方向上两个最近的邻居 如果分配给每个单元格和索引定义如下 那么问题就非常简单 idx cell idx N idy 其中 N 是网格中单元格的总数 i
  • 用于测试的 Java 编写的嵌入式 Kerberos 服务器

    有谁知道任何嵌入式 Kerberos 服务器 KDC KAdmin 它们是用 Java 编写的 并且可以仅在 JVM 进程中运行 例如 Hadoop minicluster 或嵌入式 LDAP 服务器 我的目标是让人们运行需要 Kerber
  • 当涉及到 pluck 时,to_sql 不起作用

    当我到to sql在以下查询中它工作正常 2 1 8 017 gt Task joins recurrence group recurrences id to sql gt SELECT tasks FROM tasks INNER JOI
  • XSLT:递归映射

    我是 XSLT 转换的新手 并且陷入了这种递归映射的困境