首页 > 解决方案 > 使用样式表 ( xsl ) 清理 xml

问题描述

我将使用 xml 样式表进行清理,我的问题是我无法获取节点,并且再次输出它没有任何反应。:-D

<?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:xsd="http://www.w3.org/2001/XMLSchema" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule: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">
    <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>237997</cbc:ID>
    <cbc:IssueDate>2021-04-16</cbc:IssueDate>
    <cbc:InvoiceTypeCode listID="urn:oioubl:codelist:invoicetypecode-1.1" listAgencyID="320">380</cbc:InvoiceTypeCode>
    <cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
    <cbc:AccountingCostCode>
</cbc:AccountingCostCode>
</Invoice>

我只需要一个我可以开始的 xslt 样式表,这样我就可以看到样式表应该是什么样子,所以我需要帮助使那里的 xml 看起来像这样,干净而简单。希望可以有人帮帮我。

延斯·安徒生

<?xml version="1.0" encoding="UTF-8"?>
<!--XML file created by Microsoft Dynamics C5-->
<Invoice>
    <ID>237997</ID>
    <IssueDate>2021-04-16</IssueDate>
    <InvoiceTypeCode>380</InvoiceTypeCode>
    <DocumentCurrencyCode>DKK</DocumentCurrencyCode>
</Invoice>

样式看起来像这样

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:ms="http://www.test.com/schemas/test" 
xmlns:ns="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="xsi ms ns xsl">
    <xsl:output encoding="UTF-8" indent="yes" method="xml"/>
    <xsl:template match="/">
    <xsl:apply-templates select="@*"/><!-- copy some elements in a specific order  -->
        <xsl:apply-templates/>
            <order>
                <variables>
                        <variable name="ID">
                        <xsl:value-of select="Invoice/ID"/>
                        </variable>
                    <variable name="IssueDate">
                        <xsl:value-of select="Invoice/IssueDate"/>
                    </variable>
                    <variable name="InvoiceTypeCode">
                        <xsl:value-of select="Invoice/InvoiceTypeCode"/>
                    </variable>
                </variables>
            </order>
    </xsl:template>

</xsl:stylesheet>

编辑:感谢所有提示和链接,我找到了解决方案,它是

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule: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:wix="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"

>




<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:template match="/">
<Order>
    <FrontendID>KOMITEEN FOR SUNDHED</FrontendID>
    <CustomerID>14035338</CustomerID>
    <Action>PlaceOrder</Action>
    <Shipment>
     <ContactName><xsl:value-of select="wix:Invoice/cac:BuyerCustomerParty/cac:Party/cac:Contact/cbc:ID"/></ContactName>
     <Company><xsl:value-of select="wix:Invoice/cac:BuyerCustomerParty/cac:Party/cac:PartyName/cbc:Name"/></Company>
     <Address><xsl:value-of select="wix:Invoice/cac:BuyerCustomerParty/cac:Party/cac:PostalAddress/cbc:StreetName"/></Address>
     <PostCode><xsl:value-of select="wix:Invoice/cac:BuyerCustomerParty/cac:Party/cac:PostalAddress/cbc:PostalZone"/></PostCode>
     <City><xsl:value-of select="wix:Invoice/cac:BuyerCustomerParty/cac:Party/cac:PostalAddress/cbc:CityName"/></City>
     <CountryCode><xsl:value-of select="wix:Invoice/cac:BuyerCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode"/></CountryCode>
    </Shipment>
      <Payment>
      <ReferenceNo><xsl:value-of select="wix:Invoice/cac:OrderReference/cbc:SalesOrderID"/></ReferenceNo>
    </Payment>
    <OrderLines>
    
   
        <xsl:for-each select="wix:Invoice/cac:InvoiceLine">

    
     <xsl:if test="cac:Item/cac:SellersItemIdentification/cbc:ID!='1002' and cac:Item/cac:SellersItemIdentification/cbc:ID!='1005-2'">

   
      <OrderLine>
        <ItemNo>KFS-<xsl:value-of select="cac:Item/cac:SellersItemIdentification/cbc:ID"/></ItemNo>
        <Productname><xsl:value-of select="cac:Item/cbc:Description"/></Productname>
        <Quantity><xsl:value-of select="cbc:InvoicedQuantity"/></Quantity>
      </OrderLine>
    </xsl:if>

      </xsl:for-each>
    </OrderLines>
    
    

</Order>
</xsl:template>
</xsl:stylesheet>

标签: xmlxsltxslt-1.0stylesheet

解决方案


推荐阅读