2

好吧,我有这个 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 中没有显示),主要是File在末尾添加一个新节点VirtualHardwareSection

XSL

<?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>

但是,当我使用 Altova XMLSpy 2013 转换 XML 时,我得到了这样的结果:

<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>

我不希望xmlns=""零件在那里,只需复制已经存在的节点即可。我能做些什么?

谢谢

4

1 回答 1

1

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

如果您删除xpath-default-namespaceand 使用前缀,它会改善问题吗?

于 2013-10-24T09:39:18.243 回答