将节点添加到XML而不复制xmlns =“”属性

时间:2013-10-24 09:23:02

标签: xml xslt namespaces

好吧,我有这个XML

XML

<?xml version="1.0" encoding="UTF-8"?>
<!-- Built using IBM Image Construction and Composition Tool, version: 1.2.0.1-20121129-1310-255 on: Oct 18, 2013 12:14:22 -->
<Envelope
    xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1"
    xmlns:cloudburst="http://www.ibm.com/websphere/rainmaker/2009/3" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
    xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" cloudburst:name="POSTGRES-9.2.4-RHEL-64.X64.ATF.PRB"
    cloudburst:version="1.0.0" cloudburst:build="sample" cloudburst:serviceLevel="0"
    cloudburst:description="BASEIMAGE FOR POSTGRESQL 9.2.4" cloudburst:symbolicName="POSTGRES-9.2.4-RHEL-64.X64.ATF.PRB">
  (...)

  <VirtualSystem ovf:id="POSTGRES-9.2.4-RHEL-64.X64.ATF.PRB.BASE">

    <Name>BASEIMAGE FOR POSTGRESQL 9.2.4</Name>

    <VirtualHardwareSection ovf:transport="iso">
      (...)
      <Item>
        <rasd:Caption>2 virtual CPUs</rasd:Caption>
        <rasd:Description>Number of virtual CPUs</rasd:Description>
        <rasd:ElementName>2 virtual CPUs</rasd:ElementName>
        <rasd:InstanceID>1</rasd:InstanceID>
        <rasd:ResourceType>3</rasd:ResourceType>
        <rasd:VirtualQuantity>2</rasd:VirtualQuantity>
      </Item>
      <Item>
        <rasd:Caption>SCSI Controller 0</rasd:Caption>
        <rasd:ElementName>SCSI Controller 0</rasd:ElementName>
        <rasd:InstanceID>8</rasd:InstanceID>
        <rasd:ResourceSubType>lsilogic</rasd:ResourceSubType>
        <rasd:ResourceType>6</rasd:ResourceType>
      </Item>

我想通过编辑一些Item标签和属性来修改(已经完成,未在XSL中显示),并且主要在{{1}的末尾添加新的File节点}

XSL

VirtualHardwareSection

然而,当我使用Altova XMLSpy 2013转换XML时,我最终得到了这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns="http://schemas.dmtf.org/ovf/envelope/1"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData"
xpath-default-namespace="http://schemas.dmtf.org/ovf/envelope/1" 
exclude-result-prefixes="ns"  > <!-- Para que no copie el ns: y su URI en NetworkSection -->

    <xsl:output method="xml"  indent="yes"/>
    <!--<xsl:preserve-space elements="*"/>-->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <!-- Añadimos un nodo a VirtualHardwareSection --> 
    <xsl:template match="Envelope/VirtualSystem/VirtualHardwareSection">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/> <!-- Copiamos lo que ya está incluido en el nodo -->
                <!-- *** Lo copia pero añade un atributo vacio: xmlns=""  -->
            <Item>                                                                                              <!-- v  Añadimos las siguientes líneas: v   -->
                <rasd:AddressOnParent>0</rasd:AddressOnParent>
                <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation>
                <rasd:Caption>Ethernet adapter on &quot;Network 2&quot;</rasd:Caption>
                <rasd:Connection>Network 2</rasd:Connection>
                <rasd:ElementName>eth1</rasd:ElementName>
                <rasd:InstanceID>13</rasd:InstanceID>
                <rasd:ResourceSubType>VMXNET3</rasd:ResourceSubType>
                <rasd:ResourceType>10</rasd:ResourceType>
            </Item>                                                                                             <!-- ^  Añadimos las siguientes líneas: ^   -->

        </xsl:copy>     <!-- lo unico que lo coloca al final del nodo VirtualHardwareSection, estaria bien que pudiera ponerse despues del nodo con igual ResourceType -->
    </xsl:template>

我不希望<Item> <rasd:AddressOnParent xmlns="">0</rasd:AddressOnParent> <rasd:AutomaticAllocation xmlns="">true</rasd:AutomaticAllocation> <rasd:Caption xmlns="">Ethernet adapter on "Network 2"</rasd:Caption> <rasd:Connection xmlns="">Network 2</rasd:Connection> <rasd:ElementName xmlns="">eth1</rasd:ElementName> <rasd:InstanceID xmlns="">13</rasd:InstanceID> <rasd:ResourceSubType xmlns="">VMXNET3</rasd:ResourceSubType> <rasd:ResourceType xmlns="">10</rasd:ResourceType> </Item> 部分存在,只需复制那些已存在的节点。我该怎么办?

由于

1 个答案:

答案 0 :(得分:1)

我已经尝试过使用Saxon 9 HE进行转换,并且添加额外的命名空间声明,所以这可能是XMLSpy的一个奇怪之处,也可能是您转换的其他部分的人工制品没有在问题中显示。值得注意的是,在这种情况下,由于所讨论的元素具有前缀名称而没有子元素,xmlns=""声明不会对生成的XML的使用者造成任何问题(除了它看起来对人类读者有点难看) )。

如果删除xpath-default-namespace并使用前缀代替吗?

,是否会改善问题?