删除节点级

时间:2017-01-05 08:12:14

标签: node.js xml xslt

下面是我输入的xml代码

<?xml version="1.0" encoding="UTF-8"?>
<EPCISDocument xmlns:ns1="http://apse.com" schemaVersion="" creationDate="">
<EPCISHeader>
    <StandardBusinessDocumentHeader>
        <HeaderVersion/>
        <Sender>
            <Identifier Authority=""/>
            <ContactInformation>
                <Contact/>
                <EmailAddress/>
                <FaxNumber/>
                <TelephoneNumber/>
                <ContactTypeIdentifier/>
            </ContactInformation>
        </Sender>
        <Receiver>
            <Identifier Authority=""/>
            <ContactInformation>
                <Contact/>
                <EmailAddress/>
                <FaxNumber/>
                <TelephoneNumber/>
                <ContactTypeIdentifier/>
            </ContactInformation>
        </Receiver>
        <Manifest>
            <NumberOfItems/>
            <ManifestItem>
                <MimeTypeQualifierCode/>
                <UniformResourceIdentifier/>
                <Description/>
                <LanguageCode/>
            </ManifestItem>
        </Manifest>
        <BusinessScope>
            <Scope>
                <BusinessService>
                    <BusinessServiceName/>
                    <ServiceTransaction TypeOfServiceTransaction="" IsNonRepudiationRequired="" IsAuthenticationRequired="" IsNonRepudiationOfReceiptRequired="" IsIntegrityCheckRequired="" IsApplicationErrorResponseRequired="" TimeToAcknowledgeReceipt="" TimeToAcknowledgeAcceptance="" TimeToPerform=""/>
                </BusinessService>
                <CorrelationInformation>
                    <RequestingDocumentCreationDateTime/>
                    <RequestingDocumentInstanceIdentifier/>
                    <ExpectedResponseDateTime/>
                </CorrelationInformation>
            </Scope>
        </BusinessScope>
    </StandardBusinessDocumentHeader>
</EPCISHeader>
<EPCISBody>
    <EventList>
        <ObjectEvent>
            <eventTime/>
            <recordTime/>
            <eventTimeZoneOffset/>
            <epcList>
                <epc type=""/>
            </epcList>
            <action/>
            <bizStep/>
            <disposition/>
            <readPoint>
                <id/>
            </readPoint>
            <bizLocation>
                <id/>
            </bizLocation>
            <bizTransactionList>
                <bizTransaction type=""/>
            </bizTransactionList>
            <GskEpcExtension>
                <manufacturingDate>1234</manufacturingDate>
            </GskEpcExtension>
        </ObjectEvent>
    </EventList>
</EPCISBody>
</EPCISDocument>

我想添加一些前缀,所以我已经应用了下面的xslt代码,它来自stackoverflow并且工作正常。但是使用下面的代码,我在节点级别获得名称空间。下面是xsl代码。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gsk="http://apse.com">
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="GskEpcExtension|GskEpcExtension//*">
    <xsl:element name="gsk:{name()}" namespace="http://epcis.gsk.com">
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
</xsl:template>
<xsl:template match="StandardBusinessDocumentHeader|StandardBusinessDocumentHeader//*">
    <xsl:element name="sbdh:{name()}" namespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
        <xsl:copy-of select="namespace::*"/>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
</xsl:template>

此xsl代码的输出是

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Untitled3.xslt"?>
<EPCISDocument xmlns:ns1="http://apse.com" schemaVersion="" creationDate="">
<EPCISHeader>
    <sbdh:StandardBusinessDocumentHeader xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
        <sbdh:HeaderVersion/>
        <sbdh:Sender>
            <sbdh:Identifier Authority=""/>
            <sbdh:ContactInformation>
                <sbdh:Contact/>
                <sbdh:EmailAddress/>
                <sbdh:FaxNumber/>
                <sbdh:TelephoneNumber/>
                <sbdh:ContactTypeIdentifier/>
            </sbdh:ContactInformation>
        </sbdh:Sender>
        <sbdh:Receiver>
            <sbdh:Identifier Authority=""/>
            <sbdh:ContactInformation>
                <sbdh:Contact/>
                <sbdh:EmailAddress/>
                <sbdh:FaxNumber/>
                <sbdh:TelephoneNumber/>
                <sbdh:ContactTypeIdentifier/>
            </sbdh:ContactInformation>
        </sbdh:Receiver>
        <sbdh:Manifest>
            <sbdh:NumberOfItems/>
            <sbdh:ManifestItem>
                <sbdh:MimeTypeQualifierCode/>
                <sbdh:UniformResourceIdentifier/>
                <sbdh:Description/>
                <sbdh:LanguageCode/>
            </sbdh:ManifestItem>
        </sbdh:Manifest>
        <sbdh:BusinessScope>
            <sbdh:Scope>
                <sbdh:BusinessService>
                    <sbdh:BusinessServiceName/>
                    <sbdh:ServiceTransaction TypeOfServiceTransaction="" IsNonRepudiationRequired="" IsAuthenticationRequired="" IsNonRepudiationOfReceiptRequired="" IsIntegrityCheckRequired="" IsApplicationErrorResponseRequired="" TimeToAcknowledgeReceipt="" TimeToAcknowledgeAcceptance="" TimeToPerform=""/>
                </sbdh:BusinessService>
                <sbdh:CorrelationInformation>
                    <sbdh:RequestingDocumentCreationDateTime/>
                    <sbdh:RequestingDocumentInstanceIdentifier/>
                    <sbdh:ExpectedResponseDateTime/>
                </sbdh:CorrelationInformation>
            </sbdh:Scope>
        </sbdh:BusinessScope>
    </sbdh:StandardBusinessDocumentHeader>
</EPCISHeader>
<EPCISBody>
    <EventList>
        <ObjectEvent>
            <eventTime/>
            <recordTime/>
            <eventTimeZoneOffset/>
            <epcList>
                <epc type=""/>
            </epcList>
            <action/>
            <bizStep/>
            <disposition/>
            <readPoint>
                <id/>
            </readPoint>
            <bizLocation>
                <id/>
            </bizLocation>
            <bizTransactionList>
                <bizTransaction type=""/>
            </bizTransactionList>
            <gsk:GskEpcExtension xmlns:gsk="http://epcis.gsk.com">
                <gsk:manufacturingDate>1234</gsk:manufacturingDate>
            </gsk:GskEpcExtension>
        </ObjectEvent>

在gskepcextension和standardbusinessdocuemnt标头的输出有效负载中,命名空间正在出现但我需要以下格式输出两个节点,整个输出结构如下所述。请检查并提供帮助。

     <?xml version="1.0" encoding="UTF-8"?>
 <epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:1"       xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"       xmlns:gsk="http://epcis.gsk.com" schemaVersion="1.0" creationDate="2013-05-16T13:23:36Z"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<EPCISHeader>
    <sbdh:StandardBusinessDocumentHeader>
        <sbdh:HeaderVersion/>
        <sbdh:Sender>
            <sbdh:Identifier Authority="String">5051150024380</sbdh:Identifier>
            <!-- This is optional information -->
            <sbdh:ContactInformation>
                <sbdh:Contact>String</sbdh:Contact>
                <sbdh:EmailAddress>String</sbdh:EmailAddress>
                <sbdh:FaxNumber>String</sbdh:FaxNumber>
                <sbdh:TelephoneNumber>String</sbdh:TelephoneNumber>
                <sbdh:ContactTypeIdentifier>String</sbdh:ContactTypeIdentifier>
            </sbdh:ContactInformation>
        </sbdh:Sender>
        <sbdh:Receiver>
            <sbdh:Identifier Authority="String">5051150018624</sbdh:Identifier>
            <!-- This is optional information -->
            <sbdh:ContactInformation>
                <sbdh:Contact>String</sbdh:Contact>
                <sbdh:EmailAddress>String</sbdh:EmailAddress>
                <sbdh:FaxNumber>String</sbdh:FaxNumber>
                <sbdh:TelephoneNumber>String</sbdh:TelephoneNumber>
                <sbdh:ContactTypeIdentifier>String</sbdh:ContactTypeIdentifier>
            </sbdh:ContactInformation>
        </sbdh:Receiver>
        <sbdh:DocumentIdentification>
            <sbdh:Standard/>
            <sbdh:TypeVersion/>
            <sbdh:InstanceIdentifier>39cf981e070111e6838b0000362219a6</sbdh:InstanceIdentifier>
            <sbdh:Type/>
            <sbdh:CreationDateAndTime>2001-12-17T09:30:47Z</sbdh:CreationDateAndTime>
        </sbdh:DocumentIdentification>
    </sbdh:StandardBusinessDocumentHeader>
</EPCISHeader>
<EPCISBody>
    <EventList>
        <ObjectEvent>
            <eventTime>2015-10-21T17:31:12.1530000+00:00</eventTime>
            <eventTimeZoneOffset>+02:00</eventTimeZoneOffset>
            <epcList>
                <epc>urn:epc:id:sgtin:8806500.000122.2F52DTV751</epc>
                <epc>urn:epc:id:sgtin:8806500.000122.23037KDFCV</epc>
                <epc>urn:epc:id:sgtin:8806500.000122.2RPZF7GAAB</epc>
                <epc>urn:epc:id:sgtin:8806500.000122.2HSWA43EV2</epc>
                <epc>urn:epc:id:sgtin:8806500.000122.2S0Z8P3VHV</epc>
            </epcList>
            <action>ADD</action>
            <bizStep>urn:epcglobal:cbv:bizstep:commissioning</bizStep>
            <disposition>urn:epcglobal:cbv:disp:active</disposition>
            <bizLocation>
                <id>urn:epc:id:sgln:4028685.00000.0</id>
            </bizLocation>
            <gsk:GskEpcExtension>
                <gsk:additionalTradeItemIdentificationValue>199610</gsk:additionalTradeItemIdentificationValue>
                <gsk:lotNumber>15L011</gsk:lotNumber>
                <gsk:itemExpirationDate>20171006</gsk:itemExpirationDate>
                <gsk:manufacturingDate/>
                <gsk:gtin>08806500001224</gsk:gtin>
                <gsk:nhrn> </gsk:nhrn>
            </gsk:GskEpcExtension>
        </ObjectEvent>
    </EventList>
</EPCISBody>
</epcis:EPCISDocument>

1 个答案:

答案 0 :(得分:0)

您似乎只需要再添加一个模板:

<xsl:template match="EPCISDocument">
    <epcis:EPCISDocument xmlns:epcis="urn:epcglobal:epcis:xsd:1">
        <xsl:apply-templates select="node()|@*"/>
    </epcis:EPCISDocument>
</xsl:template>

我还建议将所有命名空间声明移到顶级元素,这样最终你的样式表看起来像:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:epcis="urn:epcglobal:epcis:xsd:1"
xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
xmlns:gsk="http://epcis.gsk.com">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="EPCISDocument">
    <epcis:EPCISDocument>
        <xsl:apply-templates select="node()|@*"/>
    </epcis:EPCISDocument>
</xsl:template>

<xsl:template match="GskEpcExtension|GskEpcExtension//*">
    <xsl:element name="gsk:{name()}">
        <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
</xsl:template>

<xsl:template match="StandardBusinessDocumentHeader|StandardBusinessDocumentHeader//*">
    <xsl:element name="sbdh:{name()}">
        <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>