首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在合并旧节点和生成新的UUID元素的同时转换XML

如何在合并旧节点和生成新的UUID元素的同时转换XML
EN

Stack Overflow用户
提问于 2012-04-03 15:09:48
回答 1查看 144关注 0票数 0

我有以下XML文件

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dictionaryEntry type="CM_Vserver">
    <uuid>27bfb32f-baa1-4571-abb5-c644c132ceea</uuid>
    <object-attributes>
        <object-attribute type="String" naturalKey="false" name="admin_state">
            <description>some text</description>
        </object-attribute>
    </object-attributes>
    <object-references>
        <object-reference refCol="cluster_id" naturalKey="true" name="cluster">
            <type>49f5f09e-dcae-4922-98aa-0b4a58f27fda</type>
            <description>some text</description>
        </object-reference>
    </object-references>
</dictionaryEntry>

我想将它转换为以下XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dictionaryEntry type="CM_Vserver">
    <uuid>27bfb32f-baa1-4571-abb5-c644c132ceea</uuid>
    <dictionary-entry-properties>
        <dictionary-entry-property xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="objectAttribute" attributeType="String" name="admin_state" naturalKey="false">
            <uuid>9ccbbd60-0e62-4ae7-b158-e6825441f987</uuid>
            <description>some text</description>
        </dictionary-entry-property>
        <dictionary-entry-property xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="objectReference" referredColumn="cluster_id" name="cluster" naturalKey="true">
            <uuid>afa8c22d-5af9-424e-af6d-106ad96dadbd</uuid>
            <referenceType>49f5f09e-dcae-4922-98aa-0b4a58f27fda</referenceType>
            <description>some text</description>
        </dictionary-entry-property>
    </dictionary-entry-properties>
</dictionaryEntry>

请注意,我需要进行以下更改:

  1. 将对象-属性重命名为字典-条目-属性
  2. 将对象重命名为字典引用条目属性
  3. 将对象-引用/对象-引用/类型重命名为referenceType
  4. 在单节点中合并ex对象引用和ex对象引用,称为字典条目属性。
  5. 为每个对象生成UUID -引用

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2012-05-21 21:01:12

在我回答之前,有两件事需要调和:

首先:您还没有描述在所需的XML中出现的所有更改。例如,<dictionary-entry-property>元素似乎获得了以前丢失的名称空间;另外,一些属性名也会更改。由于您没有在您的更改列表中指定这些内容,所以我将忽略它们;让我知道它们最终是重要的,我将修改我的答案。

第二:您是否出于特定原因需要UUID?还是这个值只需要是唯一的呢?

如果您回答了前者,请参阅此链接,以讨论如何做到这一点:XSLT生成UUID。简而言之,您很可能必须依赖外部方法,例如EXSLT库或用FXSL产生随机数

另一方面,如果您回答了后者,the generate-id()函数将帮助您(我在回答中使用了这种方法)。在这两种情况下,请考虑以下两种情况:

当该XSLT针对您提供的XML:运行时,

代码语言:javascript
复制
<?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="*"/>

  <!-- Identity Template: copies everything as-is -->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <!-- Add new <dictionary-entry-properties> element to -->
  <!-- contain all of our result elements -->
  <xsl:template match="dictionaryEntry">
    <xsl:copy>
      <dictionary-entry-properties>
        <xsl:apply-templates/>
      </dictionary-entry-properties>
    </xsl:copy>
  </xsl:template>

  <!-- Replace <object-attributes> nodes with -->
  <!-- <dictionary-entry-properties> nodes -->
  <xsl:template match="object-attributes">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Replace <object-attribute> and <object-reference> elements -->
  <!-- with a new <dictionary-entry-property> element; additionally, -->
  <!-- create a <uuid> element with a unique value -->
  <xsl:template match="object-attribute | object-reference">
    <dictionary-entry-property>
      <xsl:apply-templates select="@*"/>
      <uuid>
        <xsl:value-of select="generate-id()"/>
      </uuid>
      <xsl:apply-templates/>
    </dictionary-entry-property>
  </xsl:template>

  <!-- Rename <type> elements (with <object-reference> parents -->
  <!-- and <object-references> grandparents) to <referenceType> -->
  <xsl:template match="object-references/object-reference/type">
    <referenceType>
      <xsl:apply-templates/>
    </referenceType>
  </xsl:template>

  <!-- <object-references> elements should be removed altogether -->
  <xsl:template match="object-references">
    <xsl:apply-templates/>
  </xsl:template>

</xsl:stylesheet>

这个XML是生成的:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<dictionaryEntry>
  <dictionary-entry-properties>
    <uuid>27bfb32f-baa1-4571-abb5-c644c132ceea</uuid>
    <dictionary-entry-property type="String" naturalKey="false" name="admin_state">
      <uuid>i__21420544_12</uuid>
      <description>some text</description>
    </dictionary-entry-property>
    <dictionary-entry-property refCol="cluster_id" naturalKey="true" name="cluster">
      <uuid>i__21420544_25</uuid>
      <referenceType>49f5f09e-dcae-4922-98aa-0b4a58f27fda</referenceType>
      <description>some text</description>
    </dictionary-entry-property>
  </dictionary-entry-properties>
</dictionaryEntry>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9996177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档