我有以下XML文件
<?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:
<?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>请注意,我需要进行以下更改:
提前谢谢。
发布于 2012-05-21 21:01:12
在我回答之前,有两件事需要调和:
首先:您还没有描述在所需的XML中出现的所有更改。例如,<dictionary-entry-property>元素似乎获得了以前丢失的名称空间;另外,一些属性名也会更改。由于您没有在您的更改列表中指定这些内容,所以我将忽略它们;让我知道它们最终是重要的,我将修改我的答案。
第二:您是否出于特定原因需要UUID?还是这个值只需要是唯一的呢?
如果您回答了前者,请参阅此链接,以讨论如何做到这一点:XSLT生成UUID。简而言之,您很可能必须依赖外部方法,例如EXSLT库或用FXSL产生随机数。
另一方面,如果您回答了后者,the generate-id()函数将帮助您(我在回答中使用了这种方法)。在这两种情况下,请考虑以下两种情况:
当该XSLT针对您提供的XML:运行时,
<?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是生成的:
<?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>https://stackoverflow.com/questions/9996177
复制相似问题