我想要创建XSLT来复制相同的输入XML,但是要将一些元素从一个标记复制到同级标记。
这是我的输入文件http://pastebin.com/7hVmRcqN
我希望输出与输入文件相同,但以下标签中的内容除外。<cac:Delivery>....</cac:Delivery>
从<cac:Delivery><cac:Address> .... </cac:Address>
复制所有元素并放入新创建的标签中
<cac:DeliveryParty><cac:PostalAddress> ... </cac:PostalAddress></cac:DeliveryParty>
在这里,<cac:DeliveryParty>
已经存在于输入XML中。需要在<cac:PostalAddress> ... </cac:PostalAddress>
中创建新的标记<cac:DeliveryParty>... </cac:DeliveryParty>
,而来自<cac:Delivery><cac:Address> .... </cac:Address>
的所有标记都将在<cac:PostalAddress> ... </cac:PostalAddress>
中复制。
标记及其原始输入文件中的所有内容。
<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>
因此,更新后的<cac:Delivery>..</cac:Delivery>
标记如下所示。
<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 & 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 & 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 & 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>
如能提供任何帮助,将不胜感激。
谢谢
发布于 2016-03-30 21:36:30
可以使用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>
发布于 2016-03-31 21:03:16
在回答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>
https://stackoverflow.com/questions/36301208
复制相似问题