使用 XSLT 向上移动节点

2024-05-05

我已经做了很多搜索,但我无法弄清楚如何准确地使用模板。

我的输入数据称为 DEBTORS.xml:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 001" status="A" type="C">
  <Name>Name</Name>
    <Contacts>
   <Contact default="1" gender="M" status="A">
    <Note>Patient: 1</Note>
    <FirstName></FirstName>
    <Addresses>
     <Address type="D" desc="">
      <AddressLine1>Street</AddressLine1>
      <AddressLine2></AddressLine2>
      <AddressLine3></AddressLine3>
      <PostalCode>0000 AA</PostalCode>
      <City>&apos;City</City>
      <Country code="NL"/>
      <Phone></Phone>
      <Fax></Fax>
     </Address>
     </Addresses>
    <Language code="NL"/>
    <JobDescription>--</JobDescription>
    <Phone></Phone>
    <PhoneExt></PhoneExt>
    <Fax></Fax>
    <Mobile></Mobile>
    <Email></Email>
    <WebAccess>0</WebAccess>
     </Contact>
  </Contacts>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

我的 XSL 名为 Test.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Indentation in XSL -->
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- Removing blank lines in XSL -->
<xsl:strip-space  elements="*"/>

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

<!-- special rules ... -->
    <xsl:template match="Contact">
        <xsl:copy>
                <!--
                Apply the attributes of the current node and the attributes of all
                childs
                -->
                <xsl:apply-templates select="@* | child::node()[not(self::Note)]"/>
        </xsl:copy>
    <xsl:apply-templates select="Note"/>
</xsl:template>

</xsl:stylesheet>

Wanted output:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 868" status="A" type="C">
  <Name>Name</Name>
    <Contacts>
   <Contact default="1" gender="M" status="A">
    <FirstName></FirstName>
    <Addresses>
     <Address type="D" desc="">
      <AddressLine1>Street</AddressLine1>
      <AddressLine2></AddressLine2>
      <AddressLine3></AddressLine3>
      <PostalCode>0000 AA</PostalCode>
      <City>&apos;City</City>
      <Country code="NL"/>
      <Phone></Phone>
      <Fax></Fax>
     </Address>
     </Addresses>
    <Language code="NL"/>
    <JobDescription>--</JobDescription>
    <Phone></Phone>
    <PhoneExt></PhoneExt>
    <Fax></Fax>
    <Mobile></Mobile>
    <Email></Email>
    <WebAccess>0</WebAccess>
     </Contact>
  </Contacts>
  <Note>Patient: 1</Note>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

我的问题是我的 XSL 节点“Note“作为联系人的子项出现,但我希望它作为帐户的子项。希望有人能帮助我吗?


我的问题是,对于我的 XSL,节点“Note”作为以下节点的子节点: 联系方式,但我希望它作为帐户的子帐户。

那么你需要exclude它来自Contact, and include it in Account:

XSLT 1.0

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

<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

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

<!-- special rules ... -->
<xsl:template match="Contact">
    <xsl:copy>
        <!-- exclude Note -->
        <xsl:apply-templates select="@* | node()[not(self::Note)]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Account">
    <xsl:copy>
        <!-- include Note -->
        <xsl:apply-templates select="@* | node() | Contacts/Contact/Note"/>
    </xsl:copy>
</xsl:template>

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

使用 XSLT 向上移动节点 的相关文章

随机推荐

  • 使用 Android Intent 发送基于 HTML 的电子邮件正文

    我需要使用 android Intent extra 将基于 HTML 的内容发送到电子邮件应用程序 它接受一些标签 例如 br 但它没有显示任何锚链接或 h1 其显示像简单的文本 EDITED I have tried with gmai
  • 如何使用生成器遍历文件系统?

    我正在尝试创建一个实用程序类来遍历目录中的所有文件 包括子目录和子子目录中的文件 我尝试使用发电机 因为发电机很酷 然而 我遇到了困难 def grab files directory for name in os listdir dire
  • Pymacs 助手在 30 秒后未启动

    我见过其他关于此的问题 但没有一个得到真正的回答 而且没有一个是我的问题 我有一个新系统 emacs 23 1 Centos 6 2 我认为 我下载了最新的 pymacs 并安装了它 但是 我得到 error Pymacs helper d
  • 发布到 Firebase 时,单元格会重复多次

    我正在制作一个可以发布消息的应用程序 我将数据存储在 Firebase 中 当我发布新消息时 该特定单元格会重复多次 因此我会看到该帖子 2 3 4 次或更多次 但这只是 Firebase 中的一篇文章 当我刷新屏幕 例如对数据进行排序 时
  • 通过 API Gateway 使用表单数据将图像发布到 Lambda 函数会导致文件无效

    I ve a 用 Go 编写的 Lambda 函数 https github com mhausenblas imgn blob master functions app uploadimg main go应该允许图像文件上传 通过 HTM
  • 仅使用 1 行 C++ 初始化 2d 向量

    我需要能够初始化一个 2D 向量 int同一条线我在其中创建它 更具体地说 我必须创建一个3x2大小 2D 向量并将其所有值设置为 0 仅使用1行代码 有没有一种方法可以在不使用 for 循环和几行代码的情况下完成此操作 尝试这个 std
  • clang-tidy - 忽略第三方标头代码

    我正在为我的项目使用 CMake 并且我想向项目引入 clang tidy 检查 我用于此目的CMAKE CXX CLANG TIDY and clang tidy用于检查设置的文件 我想在 CI 中使用警告作为错误来可靠地检查提交是否引入
  • Swing 是否支持 Windows 7 风格的文件选择器?

    我刚刚添加了一个标准 打开文件 与我正在编写的一个小型桌面应用程序的对话 基于JFileChooserSwing 教程的入口 http download oracle com javase tutorial uiswing componen
  • IOS:使用@property释放变量(非原子,保留)

    带有 property 非原子和保留的变量是否应该显式释放 interface MyScreenViewController UIViewController
  • 循环遍历字符串中的 html 标签并将内部文本添加到数组中

    我有一些 HTML 内容保存为字符串 我想循环遍历该字符串中的每个标题标签并获取其内部文本 let str h1 topic 1 h1 p desc of topic 1 p h1 topic 2 h1 p desc of topic 2
  • Visual Studio 2017 上的简单装配程序

    386 model flat c stack 100h printf PROTO arg1 Ptr Byte data msg1 byte Hello World 0Ah 0 code main proc INVOKE printf ADD
  • 如何允许另一个应用程序访问我的应用程序的数据目录?

    假设我有一个名为 A 的应用程序 其数据目录为 com example test 现在我想制作另一个名为 B 的应用程序来修改 com example test 中的某些内容 当然我知道两者必须共享相同的签名 但我还需要什么 基本上我正在尝
  • Java俄罗斯方块旋转

    我知道这个问题已经被问了很多 但我想知道如何旋转俄罗斯方块 我已经做了一个又长又糟糕的解决方案 大约 170 行代码 但应该有更简单的方法来做到这一点 我的俄罗斯方块由 4 个块组成 它们都知道它们在矩阵中的位置 行和列 Matrix本身是
  • 使用 PyQt4 在 QWidget 上进行 eventFilter

    我有一个 QMainWindow 其中包含DrawingPointsWidget 该小部件随机绘制红点 我通过使用以下命令为 MouseHovering 事件安装事件过滤器 在 QMainWindow 的状态栏中显示鼠标坐标self ins
  • MySQL 连接丢失:系统错误:110

    我正在尝试通过专用服务器上的 localhost 连接到 mySQL 服务器 但我不断收到错误 Lost connection to MySQL server at reading initial communication packet
  • ImageMagick,Q8 与 Q16 的实际含义是什么?

    在Windows下 我需要在Q8和Q16之间进行选择 我知道 Q8 是每像素 8 位分量 例如 8 位红色 8 位绿色等 而 Q16 是每像素 16 位分量 我还知道 Q16 使用的内存是 Q8 的两倍 因此 我必须慎重选择 什么是每像素
  • 字符串编码器固定大小输出

    我接到一个任务 需要编写一个具有以下要求的编码器 输入 1 到 8 位的整数 即 12345678 2352 76543 输出 固定大小的 6 位代码 可以包含任何字母数字和符号 a z A Z 0 9 该操作必须是可逆的 因此给定一个代码
  • 简单的线框格式?

    我正在寻找一种用于线框模型的简单文件格式 我知道 VRML u3D 等 但这些对于我的需求来说似乎很重要 我的标准是 必须有明确的规格 要么是开放的 要么是非常完善 记录的 我只需要 想要 简单的模型 顶点和边 我不想处理面孔或物体 如果格
  • 使用HTMLWebpackPlugin时如何通过webpack加载图片?

    我正在使用 HTMLWebpackPlugin 在我的模板中我有一个 img 标签 img src images logo png 如果您注意到 这里我使用相对路径 认为 webpack 将触发在 webpack config js 文件中
  • 使用 XSLT 向上移动节点

    我已经做了很多搜索 但我无法弄清楚如何准确地使用模板 我的输入数据称为 DEBTORS xml