2017-01-05 28 views
0

下面是我輸入的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的代碼,它工作正常。但使用下面的代碼,我得到了節點級別的名稱空間。下面是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> 
+0

請將示例降低到證明問題所需的最低限度,並解釋用言語來說問題是什麼。我發佈了一個答案,解決了我檢測到的一個**問題,但我不會花費時間比較您的輸出以找出所有差異。 –

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

我已經應用了上面的xsl代碼,但仍然得到相同的output.in在節點級別我得到的名稱空間。gskepcextnesion節點應該是不像

+0

正確的格式,我需要:出現不正確的格式在不需要的輸出有效載荷中。 –

+0

@janardhand在應用建議的更改時,我無法得到您所報告的結果 - 請參閱:http://xsltransform.net/ejivdHt ---在任何情況下,冗餘名稱空間聲明都不應對接收應用程序造成任何問題。 –