XML 到 XML - 创建唯一 ID 并在同一文档中引用它们

2024-03-28

我有一个源 xml,其中包含现场地址,需要转换为一个 xml,将所有地址保存到单个元素中并引用每个地址。 我正在使用 Saxon 9.1 处理器和样式表版本 1.0。 感谢您的帮助。

源代码:

<?xml version="1.0" encoding="utf-8"?>
<ContactDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <AddressDetails StartDate="1992-04-03" Type="Previous">
        <Address>
            <City City="Wien" />
            <Postcode Postcode="LSP-123" />
        </Address>
    </AddressDetails>
    <AddressDetails StartDate="1982-09-19" Type="Current">
        <Address>
            <City City="Toronto" />
            <Postcode Postcode="LKT-947" />
        </Address>
    </AddressDetails>
    <AddressDetails StartDate="1977-05-27" Type="Mailing">
        <Address>
            <City City="Sydney" />
            <Postcode Postcode="OKU-846" />
        </Address>
    </AddressDetails>
</ContactDetails>

目标代码:

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ContactDetails>
        <AddressDetails StartDate="1992-04-03" Type="Previous">
            <AddressRef ReferedID="Prev_1" />
        </AddressDetails>
        <AddressDetails StartDate="1982-09-19" Type="Current">
            <AddressRef ReferedID="Curr_2" />
        </AddressDetails>
        <AddressDetails StartDate="1977-05-27" Type="Mailing">
            <AddressRef ReferedID="Mail_3" />
        </AddressDetails>
    </ContactDetails>
    <AddressSegment>
            <Address>
                <ID ID="Prev_1" />
                <City City="Wien" />
                <Postcode Postcode="LSP-123" />
            </Address>
            <Address>
                <ID UniqueID="Curr_2" />
                <City City="Toronto" />
                <Postcode Postcode="LKT-947" />
            </Address>        
            <Address>
                <ID UniqueID="Mail_3" />
                <City City="Sydney" />
                <Postcode Postcode="OKU-846" />
            </Address>        
    </AddressSegment>
</Application>

当我尝试先生成 ID 并将其复制到地址中时,我使用了 key 和generate-id。这是我对 xslt 的最后一次尝试(我得到的最好结果是 UniqueID 为空,所以我不知道这个解决方案还有多远:))

<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="ID_key" match="*[@ReferedID]" use="@ReferedID"/>
<xsl:template match="/">
    <Application>
    <ContactDetails>
        <xsl:for-each select="Application/Person/ContactDetails/AddressDetails">
        <AddressDetails>
            <xsl:attribute name="StartDate">
            <xsl:value-of select="@StartDate"/>
            </xsl:attribute>
                        <xsl:attribute name="Type">
                            <xsl:value-of select="@Type" />
            </xsl:attribute>
                <AddressRef>
            <xsl:attribute name="ReferedID">
                <xsl:value-of select="generate-id()"/>
            </xsl:attribute>
            </AddressRef>
        </AddressDetails>
        </xsl:for-each>
    </ContactDetails>
    <AddressSegment>
        <xsl:for-each select="Application/Person/ContactDetails/AddressDetails">
        <Address>
            <ID>
            <xsl:attribute name="UniqueID">
                <xsl:value-of select="Address/ID[generate-id()=generate-id(key('ID_key',@UniqueID))]" />
            </xsl:attribute>
            </ID>
            <City>
            <xsl:attribute name="City">
                <xsl:value-of select="Address/City/@City"/>
            </xsl:attribute>
            </City>
                        <Postcode>
                                    <sl:attribute name="Postcode">
                    <xsl:value-of select="Address/Postcode/@Postcode"/>
                    </xsl:attribute>
            </Postcode>
        </Address>
            </xsl:for-each>
     </AddressSegment>
   </Application>
   </xsl:template>
</xsl:stylesheet>

为了给您提供如何使用generate-id和模式的示例,示例样式表

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

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:template match="ContactDetails">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <ContactDetails>
        <xsl:apply-templates select="AddressDetails/Address" mode="det"/>
      </ContactDetails>
      <AddressSegment>
        <xsl:apply-templates select="AddressDetails/Address"/>
      </AddressSegment>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Address" mode="det">
    <AddressDetails StartDate="{../@StartDate}" Type="{../@Type}">
      <AddressRef ReferedID="{generate-id()}"/>
    </AddressDetails>
  </xsl:template>

  <xsl:template match="Address">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <ID ID="{generate-id()}"/>
      <xsl:copy-of select="*"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

转换输入

<?xml version="1.0" encoding="utf-8"?>
<ContactDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <AddressDetails StartDate="1992-04-03" Type="Previous">
        <Address>
            <City City="Wien" />
            <Postcode Postcode="LSP-123" />
        </Address>
    </AddressDetails>
    <AddressDetails StartDate="1982-09-19" Type="Current">
        <Address>
            <City City="Toronto" />
            <Postcode Postcode="LKT-947" />
        </Address>
    </AddressDetails>
    <AddressDetails StartDate="1977-05-27" Type="Mailing">
        <Address>
            <City City="Sydney" />
            <Postcode Postcode="OKU-846" />
        </Address>
    </AddressDetails>
</ContactDetails>

将 Saxon 6.5.5 输入到输出中

<ContactDetails xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <ContactDetails>
      <AddressDetails StartDate="1992-04-03" Type="Previous">
         <AddressRef ReferedID="d0e3"/>
      </AddressDetails>
      <AddressDetails StartDate="1982-09-19" Type="Current">
         <AddressRef ReferedID="d0e7"/>
      </AddressDetails>
      <AddressDetails StartDate="1977-05-27" Type="Mailing">
         <AddressRef ReferedID="d0e11"/>
      </AddressDetails>
   </ContactDetails>
   <AddressSegment>
      <Address>
         <ID ID="d0e3"/>
         <City City="Wien"/>
         <Postcode Postcode="LSP-123"/>
      </Address>
      <Address>
         <ID ID="d0e7"/>
         <City City="Toronto"/>
         <Postcode Postcode="LKT-947"/>
      </Address>
      <Address>
         <ID ID="d0e11"/>
         <City City="Sydney"/>
         <Postcode Postcode="OKU-846"/>
      </Address>
   </AddressSegment>
</ContactDetails>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XML 到 XML - 创建唯一 ID 并在同一文档中引用它们 的相关文章

随机推荐

  • 结构化 Spark 流指标检索

    我有一个具有结构化 Spark 流的应用程序 我想获取一些指标 例如调度延迟 延迟等 通常 此类指标可以在 Spark UI Streaming 选项卡中找到 但是 结构化流不存在此类功能我知道 那么如何获取这些指标值呢 目前 我尝试使用查
  • Java 泛型:泛型映射(深拷贝)的方法签名

    我有几个Map其本身可能再次包含Maps 任何类型 我写了一个带有签名的方法 public static
  • netbeans jvi vimrc 文件位置

    我已经开始使用 netbeans vim 插件 Jvi 并且我似乎找不到 vimrc 文件位置 我发现一个选项似乎说它将把 vimrc 保存在主文件夹中 但那里没有 我正在使用Ubuntu 谢谢 jVi 不支持 vimscript 因此它不
  • 文本框的日期验证

    我一直用它来将日期设为 mm dd yyyy 格式
  • ViewModelProviders 在 1.1.0 中已弃用

    看着谷歌文档 https developer android com topic libraries architecture viewmodel for ViewModel 他们展示了下面的示例代码 说明如何获得ViewModel val
  • 观察角度指令中 ngModel.$invalid 的变化

    我有一个指令替换select具有自定义输入控件的元素 这是它的简化版本 angular module MyModule directive reflector function timeout return require ngModel
  • Android 从 youtube 获取视频链接

    您好 我正在开发一个 Android 应用程序 我的应用程序的一部分想要将歌曲标题解析到 YouTube 并获取视频链接 获得 100 正确的视频并不重要 那么我如何从 youtube 检索数据呢 任何人都可以帮助我找到解决方案 这对我来说
  • gzip 文件如何存储在 HDFS 中

    HDFS存储支持压缩格式来存储压缩文件 我知道 gzip 压缩不支持夹板 现在假设该文件是一个 gzip 压缩文件 其压缩大小为 1 GB 现在我的问题是 该文件将如何存储在 HDFS 中 块大小为 64MB 由此link http com
  • 非常奇怪的 Application.ThreadException 行为

    我正在使用应用程序线程异常 http msdn microsoft com en us library system windows forms application threadexception aspx事件来处理和记录我的 winf
  • 查找 lambda 表达式中的自由变量

    有谁知道如何找出 lambda 表达式中的自由变量 自由变量是不属于 lambda 参数的变量 我当前的方法 这对我毫无帮助 是简单地使用 car 和 cdr 来遍历表达式 我的主要问题是确定一个值是否是一个变量或者它是否是方案原语之一 有
  • 世博会:无效的 sdkVersion“32.0.0”

    在尝试使用 expo 配置根据安装指南运行反应本机应用程序时 我发现了此错误 错误 sdkVersion 无效 有效选项为 10 0 0 11 0 0 12 0 0 13 0 0 14 0 0 15 0 0 16 0 0 17 0 0 18
  • BGTaskScheduler.shared.register 未调用

    我正在使用后台刷新来安排本地通知并更新我的小部件 问题是没有调用任务注册方法 func application application UIApplication didFinishLaunchingWithOptions launchOp
  • p4v 不显示签出文件的完整列表

    如果我在 p4v 中签出 4000 个文件 它不会显示默认更改列表中的完整文件列表 它只是说已签出 4000 个文件 有什么方法可以查看 4000 个文件的完整列表吗 通过 编辑 gt 首选项 菜单打开首选项对话框 单击 服务器数据 您将看
  • Flash上​​传图片调整客户端大小

    有谁知道如何使用 Flash 调整客户端图像大小 例子 客户选择一张 1200x800 的图像 在上传之前 Flash 会将其变成一半或其他什么 有什么想法吗 Plupload 是开源的 拥有良好的文档并支持多个平台 包括 Gears 和
  • 尝试连接到 PubNub 服务时获取 PNTimeoutCategory

    得到PNTimeoutCategory尝试连接到 PubNub 服务时 以下是PNStatus我们收到的类别 PNErrorData information null throwable com pubnub api PubNubExcep
  • 是否有一个直接的解决方案可以在命中 dropWhile 谓词之前*接收元素?

    给定一个条件 我想搜索元素列表并返回满足条件的第一个元素和前一个元素 在 C C 中这很简单 int i 0 for i if arr i 0 break 当我们得到满足条件的索引后 获取前一个元素就很容易了 通过 arr i 1 在哈斯克
  • Ubuntu 14.04 桌面上的 Jenkins.log 位置

    Setup 我目前正在 Ubuntu 14 04 桌面计算机上运行 Jenkins 实例 我已经通过 WAR 发行版安装了 Jenkins 并在主机上将 Jenkins 作为服务运行 Issue 我正在尝试访问描述的 jenkins log
  • 将匿名类型作为方法参数传递

    在我的插件架构中 我当前将插件名称 字符串 方法名称 字符串 和参数 对象数组 传递给我的插件服务 以执行指定的方法并返回结果 T 类型 插件服务的执行方法如下所示 public TResult Execute
  • Pandas 中最快的计算方法?

    给定这两个数据框 df1 Name Start End 0 A 10 20 1 B 20 30 2 C 30 40 df2 0 1 0 5 10 1 15 20 2 25 30 df2没有列名 但您可以假设列 0 的偏移量为df1 Star
  • XML 到 XML - 创建唯一 ID 并在同一文档中引用它们

    我有一个源 xml 其中包含现场地址 需要转换为一个 xml 将所有地址保存到单个元素中并引用每个地址 我正在使用 Saxon 9 1 处理器和样式表版本 1 0 感谢您的帮助 源代码