【问题标题】:How to copy all tags from one Node to another Node in XSLT while other content as it is如何在 XSLT 中将所有标签从一个节点复制到另一个节点,而其他内容保持不变
【发布时间】:2016-03-30 06:24:08
【问题描述】:

我想创建 XSLT 来复制相同的输入 XML,但将一些元素从一个标记复制到同级标记。

这是我的输入文件 http://pastebin.com/7hVmRcqN

我希望输出与输入文件相同,除了以下标签内的内容。 <cac:Delivery>....</cac:Delivery><cac:Delivery><cac:Address> .... </cac:Address>复制所有元素并放入新创建的标签内

<cac:DeliveryParty><cac:PostalAddress> ... </cac:PostalAddress></cac:DeliveryParty>

这里 &lt;cac:DeliveryParty&gt; 已经存在于输入 XML 中。 需要在&lt;cac:DeliveryParty&gt;... &lt;/cac:DeliveryParty&gt; 内创建新标签&lt;cac:PostalAddress&gt; ... &lt;/cac:PostalAddress&gt; 并且来自&lt;cac:Delivery&gt;&lt;cac:Address&gt; .... &lt;/cac:Address&gt; 的所有标签都将复制到&lt;cac:PostalAddress&gt; ... &lt;/cac:PostalAddress&gt;

原始输入文件中的标记及其所有内容

<cac:Delivery>
<cac:DeliveryLocation>
  <cbc:Description>er</cbc:Description>
  <cac:Address>
    <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
    <cbc:StreetName>sadasd</cbc:StreetName>
    <cbc:CityName>asd</cbc:CityName>
    <cbc:PostalZone>6630</cbc:PostalZone>
    <cac:Country>
      <cbc:IdentificationCode>DK</cbc:IdentificationCode>
    </cac:Country>
  </cac:Address>
</cac:DeliveryLocation>
<cac:DeliveryParty>
  <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:EndpointID>
  <cac:PartyIdentification>
    <cbc:ID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:ID>
  </cac:PartyIdentification>
  <cac:PartyName>
    <cbc:Name>asd asd asd</cbc:Name>
  </cac:PartyName>
</cac:DeliveryParty>
</cac:Delivery>

所以更新后的&lt;cac:Delivery&gt;..&lt;/cac:Delivery&gt; 标签将如下所示..

<cac:Delivery>
    <cac:DeliveryLocation>
      <cbc:Description>er</cbc:Description>
      <cac:Address>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>sadasd</cbc:StreetName>
        <cbc:CityName>asd</cbc:CityName>
        <cbc:PostalZone>6630</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:Address>
    </cac:DeliveryLocation>
    <cac:DeliveryParty>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>asd asd asd</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
         <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
         <cbc:StreetName>sadasd</cbc:StreetName>
         <cbc:CityName>asd</cbc:CityName>
         <cbc:PostalZone>6630</cbc:PostalZone>
         <cac:Country>
            <cbc:IdentificationCode>DK</cbc:IdentificationCode>
         </cac:Country>
       </cac:PostalAddress>
    </cac:DeliveryParty>
  </cac:Delivery>

这是我尝试制作的 xslt 代码,但不知道下一步该做什么

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl    = "http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi    = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:oioubl = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
    xmlns:cac    = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc    = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    xmlns:ccts   = "urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2"
    xmlns:sdt    = "urn:oasis:names:specification:ubl:schema:xsd:SpecializedDatatypes-2"
    xmlns:udt    = "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2">
  <!--Identity template,  provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="cac:DeliveryParty">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:element name="cac:PostalAddress">

      </xsl:element>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

XML 的最终输出应如下所示

   <?xml version="1.0" encoding="UTF-8"?>
<!--XML file created by Microsoft Dynamics C5-->
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <cbc:UBLVersionID schemeAgencyID="320" schemeAgencyName="urn:oioubl:id:profileid-1.1">2.0</cbc:UBLVersionID>
  <cbc:CustomizationID>OIOUBL-2.02</cbc:CustomizationID>
  <cbc:ProfileID schemeID="urn:oioubl:id:profileid-1.2" schemeAgencyID="320">Procurement-BilSim-1.0</cbc:ProfileID>
  <cbc:ID>Test123</cbc:ID>
  <cbc:IssueDate>2016-03-22</cbc:IssueDate>
  <cbc:InvoiceTypeCode listID="urn:oioubl:codelist:invoicetypecode-1.1" listAgencyID="320">380</cbc:InvoiceTypeCode>
  <cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
  <cbc:AccountingCost />
  <cac:OrderReference>
    <cbc:ID>Test321</cbc:ID>
    <cbc:SalesOrderID>Test212</cbc:SalesOrderID>
    <cbc:IssueDate>2016-03-21</cbc:IssueDate>
  </cac:OrderReference>
  <cac:AccountingSupplierParty>
    <cac:Party>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">9999999999999</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="DK:CVR">DK99999999</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>F &amp; H Com. A/S</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>Tets</cbc:StreetName>
        <cbc:InhouseMail>mail@test.dk</cbc:InhouseMail>
        <cbc:CityName>test</cbc:CityName>
        <cbc:PostalZone>8751</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID schemeID="DK:SE">DK99999999</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:ID schemeID="urn:oioubl:id:taxschemeid-1.1" schemeAgencyID="320">63</cbc:ID>
          <cbc:Name>Moms</cbc:Name>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>test</cbc:RegistrationName>
        <cbc:CompanyID schemeID="DK:CVR">DK99999999</cbc:CompanyID>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:ID>ZZ</cbc:ID>
        <cbc:Name>DK99999999</cbc:Name>
        <cbc:Telephone>DK99999999</cbc:Telephone>
        <cbc:Telefax>DK99999999</cbc:Telefax>
        <cbc:ElectronicMail>DK99999999</cbc:ElectronicMail>
      </cac:Contact>
    </cac:Party>
  </cac:AccountingSupplierParty>
  <cac:AccountingCustomerParty>
    <cac:Party>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">8888888888888</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="DK:CVR">DK33333333</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>zdfsdf</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>etwf</cbc:StreetName>
        <cbc:CityName>asd</cbc:CityName>
        <cbc:PostalZone>6500</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyTaxScheme>
        <cbc:CompanyID schemeID="DK:SE">DK33333333</cbc:CompanyID>
        <cac:TaxScheme>
          <cbc:ID schemeID="urn:oioubl:id:taxschemeid-1.1" schemeAgencyID="320">63</cbc:ID>
          <cbc:Name>Moms</cbc:Name>
        </cac:TaxScheme>
      </cac:PartyTaxScheme>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>tesad</cbc:RegistrationName>
        <cbc:CompanyID schemeID="DK:CVR">DK33333333</cbc:CompanyID>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:ID>ZZ</cbc:ID>
      </cac:Contact>
    </cac:Party>
  </cac:AccountingCustomerParty>
  <cac:BuyerCustomerParty>
    <cac:Party>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5704707012544</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="DK:CVR">DK07012544</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>Davidsenshop.dk</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>Industrivej 36</cbc:StreetName>
        <cbc:InhouseMail>mail@davidsenshop.dk</cbc:InhouseMail>
        <cbc:CityName>Vamdrup</cbc:CityName>
        <cbc:PostalZone>6580</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>Davidsenshop.dk</cbc:RegistrationName>
        <cbc:CompanyID schemeID="DK:CVR">DK07012544</cbc:CompanyID>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:ID>test123</cbc:ID>
        <cbc:Telephone>78774800</cbc:Telephone>
        <cbc:ElectronicMail>mail@davidsenshop.dk</cbc:ElectronicMail>
      </cac:Contact>
    </cac:Party>
  </cac:BuyerCustomerParty>
  <cac:SellerSupplierParty>
    <cac:Party>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5790002307478</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="DK:CVR">DK28893353</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>F &amp; H Com. A/S</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>Gl. Kattrupvej 4</cbc:StreetName>
        <cbc:InhouseMail>mail@fhcom.dk</cbc:InhouseMail>
        <cbc:CityName>Gedved</cbc:CityName>
        <cbc:PostalZone>8751</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
      <cac:PartyLegalEntity>
        <cbc:RegistrationName>F &amp; H Com. A/S</cbc:RegistrationName>
        <cbc:CompanyID schemeID="DK:CVR">DK28893353</cbc:CompanyID>
      </cac:PartyLegalEntity>
      <cac:Contact>
        <cbc:ID>ZZ</cbc:ID>
        <cbc:Name>Kenneth Rix Bløcher</cbc:Name>
        <cbc:Telephone>76273744</cbc:Telephone>
        <cbc:Telefax>76273748</cbc:Telefax>
        <cbc:ElectronicMail>mail@fhcom.dk</cbc:ElectronicMail>
      </cac:Contact>
    </cac:Party>
  </cac:SellerSupplierParty>
  <cac:Delivery>
    <cac:DeliveryLocation>
      <cbc:Description>er</cbc:Description>
      <cac:Address>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>sadasd</cbc:StreetName>
        <cbc:CityName>asd</cbc:CityName>
        <cbc:PostalZone>6630</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:Address>
    </cac:DeliveryLocation>
    <cac:DeliveryParty>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:EndpointID>
      <cac:PartyIdentification>
        <cbc:ID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
        <cbc:Name>asd asd asd</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
        <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
        <cbc:StreetName>sadasd</cbc:StreetName>
        <cbc:CityName>asd</cbc:CityName>
        <cbc:PostalZone>6630</cbc:PostalZone>
        <cac:Country>
          <cbc:IdentificationCode>DK</cbc:IdentificationCode>
        </cac:Country>
      </cac:PostalAddress>
    </cac:DeliveryParty>
  </cac:Delivery>
  <cac:PaymentMeans>
    <cbc:ID>1</cbc:ID>
    <cbc:PaymentMeansCode>42</cbc:PaymentMeansCode>
    <cbc:PaymentDueDate>2016-04-15</cbc:PaymentDueDate>
    <cbc:PaymentChannelCode listID="urn:oioubl:codelist:paymentchannelcode-1.1" listAgencyID="320">DK:BANK</cbc:PaymentChannelCode>
    <cac:PayeeFinancialAccount>
      <cbc:ID>asd</cbc:ID>
      <cac:FinancialInstitutionBranch>
        <cbc:ID>test123</cbc:ID>
      </cac:FinancialInstitutionBranch>
    </cac:PayeeFinancialAccount>
  </cac:PaymentMeans>
  <cac:PaymentTerms>
    <cbc:Note>SPECIFIC</cbc:Note>
    <cbc:Amount currencyID="DKK">796.00</cbc:Amount>
  </cac:PaymentTerms>
  <cac:TaxTotal>
    <cbc:TaxAmount currencyID="DKK">159.20</cbc:TaxAmount>
    <cac:TaxSubtotal>
      <cbc:TaxableAmount currencyID="DKK">636.80</cbc:TaxableAmount>
      <cbc:TaxAmount currencyID="DKK">159.20</cbc:TaxAmount>
      <cac:TaxCategory>
        <cbc:ID schemeID="urn:oioubl:id:taxcategoryid-1.1" schemeAgencyID="320">StandardRated</cbc:ID>
        <cbc:Percent>25.00</cbc:Percent>
        <cac:TaxScheme>
          <cbc:ID schemeID="urn:oioubl:id:taxschemeid-1.1" schemeAgencyID="320">63</cbc:ID>
          <cbc:Name>Moms</cbc:Name>
        </cac:TaxScheme>
      </cac:TaxCategory>
    </cac:TaxSubtotal>
  </cac:TaxTotal>
  <cac:LegalMonetaryTotal>
    <cbc:LineExtensionAmount currencyID="DKK">636.80</cbc:LineExtensionAmount>
    <cbc:TaxExclusiveAmount currencyID="DKK">159.20</cbc:TaxExclusiveAmount>
    <cbc:TaxInclusiveAmount currencyID="DKK">796.00</cbc:TaxInclusiveAmount>
    <cbc:PayableAmount currencyID="DKK">796.00</cbc:PayableAmount>
  </cac:LegalMonetaryTotal>
  <cac:InvoiceLine>
    <cbc:ID>1</cbc:ID>
    <cbc:InvoicedQuantity unitCode="EA">1.00</cbc:InvoicedQuantity>
    <cbc:LineExtensionAmount currencyID="DKK">636.80</cbc:LineExtensionAmount>
    <cac:OrderLineReference>
      <cbc:LineID>1</cbc:LineID>
      <cac:OrderReference>
        <cbc:ID>test123</cbc:ID>
        <cbc:SalesOrderID>19070</cbc:SalesOrderID>
      </cac:OrderReference>
    </cac:OrderLineReference>
    <cac:Delivery>
      <cbc:ActualDeliveryDate>2016-03-22</cbc:ActualDeliveryDate>
    </cac:Delivery>
    <cac:TaxTotal>
      <cbc:TaxAmount currencyID="DKK">159.20</cbc:TaxAmount>
      <cac:TaxSubtotal>
        <cbc:TaxableAmount currencyID="DKK">636.80</cbc:TaxableAmount>
        <cbc:TaxAmount currencyID="DKK">159.20</cbc:TaxAmount>
        <cac:TaxCategory>
          <cbc:ID schemeID="urn:oioubl:id:taxcategoryid-1.1" schemeAgencyID="320">StandardRated</cbc:ID>
          <cbc:Percent>25.00</cbc:Percent>
          <cac:TaxScheme>
            <cbc:ID schemeID="urn:oioubl:id:taxschemeid-1.1" schemeAgencyID="320">63</cbc:ID>
            <cbc:Name>Moms</cbc:Name>
          </cac:TaxScheme>
        </cac:TaxCategory>
      </cac:TaxSubtotal>
    </cac:TaxTotal>
    <cac:Item>
      <cbc:Description>werwe</cbc:Description>
      <cbc:Name>werwer</cbc:Name>
      <cac:BuyersItemIdentification>
        <cbc:ID>10017</cbc:ID>
      </cac:BuyersItemIdentification>
      <cac:SellersItemIdentification>
        <cbc:ID>10017</cbc:ID>
      </cac:SellersItemIdentification>
    </cac:Item>
    <cac:Price>
      <cbc:PriceAmount currencyID="DKK">636.8000</cbc:PriceAmount>
      <cbc:BaseQuantity unitCode="EA">1.00</cbc:BaseQuantity>
      <cbc:OrderableUnitFactorRate>1</cbc:OrderableUnitFactorRate>
    </cac:Price>
  </cac:InvoiceLine>
</Invoice>

任何帮助将不胜感激

谢谢

【问题讨论】:

  • 请发布minimal but complete 输入 XML、XSLT 和预期输出示例。
  • 我已经包含在帖子中了..查看链接
  • 你漏掉了minimal这个词
  • 我没有得到..这个问题还不够吗。?我已尽力详细说明。
  • 更新后你的问题好多了..

标签: xml xslt copy


【解决方案1】:

您可以使用 xsl:copy-of 传递 XPath 参数,该参数引用当前 cac:Delivery 祖先中 cac:Address 的所有子级:

<xsl:element name="cac:PostalAddress">
    <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/*"/>
</xsl:element>

下面是一个完整的工作示例

XML 输入:

<cac:Delivery 
    xmlns:cac    = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc    = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
>    
<cac:DeliveryLocation>
  <cbc:Description>er</cbc:Description>
  <cac:Address>
    <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
    <cbc:StreetName>sadasd</cbc:StreetName>
    <cbc:CityName>asd</cbc:CityName>
    <cbc:PostalZone>6630</cbc:PostalZone>
    <cac:Country>
      <cbc:IdentificationCode>DK</cbc:IdentificationCode>
    </cac:Country>
  </cac:Address>
</cac:DeliveryLocation>
<cac:DeliveryParty>
  <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:EndpointID>
  <cac:PartyIdentification>
    <cbc:ID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:ID>
  </cac:PartyIdentification>
  <cac:PartyName>
    <cbc:Name>asd asd asd</cbc:Name>
  </cac:PartyName>
</cac:DeliveryParty>
</cac:Delivery>

XSLT:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl    = "http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi    = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cac    = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc    = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">

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

  <xsl:template match="cac:DeliveryParty">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:element name="cac:PostalAddress">
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/*"/>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

  <!--Identity template,  provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<cac:Delivery xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
              xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">
   <cac:DeliveryLocation>
      <cbc:Description>er</cbc:Description>
      <cac:Address>
         <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
         <cbc:StreetName>sadasd</cbc:StreetName>
         <cbc:CityName>asd</cbc:CityName>
         <cbc:PostalZone>6630</cbc:PostalZone>
         <cac:Country>
            <cbc:IdentificationCode>DK</cbc:IdentificationCode>
         </cac:Country>
      </cac:Address>
   </cac:DeliveryLocation>
   <cac:DeliveryParty>
      <cbc:EndpointID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:EndpointID>
      <cac:PartyIdentification>
         <cbc:ID schemeID="GLN" schemeAgencyID="9">5555555555555</cbc:ID>
      </cac:PartyIdentification>
      <cac:PartyName>
         <cbc:Name>asd asd asd</cbc:Name>
      </cac:PartyName>
      <cac:PostalAddress>
         <cbc:AddressFormatCode listID="urn:oioubl:codelist:addressformatcode-1.1" listAgencyID="320">StructuredLax</cbc:AddressFormatCode>
         <cbc:StreetName>sadasd</cbc:StreetName>
         <cbc:CityName>asd</cbc:CityName>
         <cbc:PostalZone>6630</cbc:PostalZone>
         <cac:Country>
            <cbc:IdentificationCode>DK</cbc:IdentificationCode>
         </cac:Country>
      </cac:PostalAddress>
   </cac:DeliveryParty>
</cac:Delivery>

【讨论】:

  • 感谢您的回答,它帮助了我并且工作得很好
【解决方案2】:

我在 har07 回答之前尝试了我的方法。 以下是我尝试过的 XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl    = "http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi    = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:oioubl = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
    xmlns:cac    = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc    = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    xmlns:ccts   = "urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2"
    xmlns:sdt    = "urn:oasis:names:specification:ubl:schema:xsd:SpecializedDatatypes-2"
    xmlns:udt    = "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2">
  <!--Identity template,  provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="cac:DeliveryParty">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:element name="cac:PostalAddress">
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/cbc:AddressFormatCode" />
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/cbc:StreetName" />
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/cbc:CityName" />
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/cbc:PostalZone" />
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/cac:Country" />
      </xsl:element>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但我认为 har07 的方法要好得多,因为它将涵盖输入 XML 中的所有标签。 而在我的 XSLT 中,它只会覆盖 XSLT 中列出的标签。

所以我更喜欢使用 har07 的 XSLT 脚本是最好的。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl    = "http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi    = "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cac    = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc    = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">

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

  <xsl:template match="cac:DeliveryParty">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      <xsl:element name="cac:PostalAddress">
        <xsl:copy-of select="../cac:DeliveryLocation/cac:Address/*"/>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

  <!--Identity template,  provides default behavior that copies all content into the output -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多