使用XSLT 2.0从元素中删除命名空间

时间:2014-11-15 04:27:31

标签: xml xslt xslt-2.0

我正在使用XSLT 2.0来转换我的XML。我想在我的输出XML中没有CMMHeader的命名空间,我尝试了所有传统的方法,但它们不起作用。所以请有人指出我做错了什么。 注意:

我只想删除带有CMMHeader标记的命名空间。

输入XML

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body><tns:GLBookingMessage xmlns:tns="http://com.example/cdm/finance/generalledger/v1"> 
<tns:GLBooking>

 <cdm:CompanyCodeTo xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">3010</cdm:CompanyCodeTo>
  <cdm:PostingDate xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">20141009</cdm:PostingDate> 
  <cdm:CreationDate xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">20140901</cdm:CreationDate>
   <cdm:GLBookingLine xmlns:cdm="http://com.example/cdm/finance/generalledger/v1"> 
   <cdm:LineNumber>1</cdm:LineNumber>
    <cdm:AccountNumber>0000133200</cdm:AccountNumber> 
    <cdm:Description> </cdm:Description> 


       </cdm:GLBookingLine> 
     <cdm:GLBookingLine xmlns:cdm="http://com.example/cdm/finance/generalledger/v1"> 
     <cdm:LineNumber>2</cdm:LineNumber> 
     <cdm:AccountNumber>0000133205</cdm:AccountNumber>
      <cdm:Description> </cdm:Description>

      </cdm:GLBookingLine>
       </tns:GLBooking> 
      </tns:GLBookingMessage>
     </soapenv:Body></soapenv:Envelope>

XSLT 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 

xmlns:cdm="http://com.example/cdm/finance/generalledger/v1" 
xmlns:tns="http://com.example/cdm/finance/generalledger/v1" 
xmlns:cur="http://com.example/cdm/currencycodes/v1"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
exclude-result-prefixes="cdm tns"
>

<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:param name="Message_ID"/>
<xsl:template match="@* | node()">
<xsl:copy inherit-namespaces="no">
<xsl:apply-templates select="@* | node()"/>

</xsl:copy>
</xsl:template>
<xsl:template match="tns:GLBookingMessage">
<xsl:copy copy-namespaces="no">


<CMMHeader>
<MessageTimeStamp>

<xsl:value-of select="format-date(current-date(), '[Y0001]-[M01]-[D01]')"/>
<xsl:text>T</xsl:text>
<xsl:value-of select="format-time(current-time(), '[H01]:[m01]:[s01].[z]')"/>
</MessageTimeStamp>
<MessageId>
<xsl:value-of select="$Message_ID"/>    
</MessageId>
<ComponentId>
<xsl:text>GLBookingFileAdapter</xsl:text>
</ComponentId>
<From>  
<xsl:text>Silta</xsl:text>
</From>
<To>
<xsl:text>GLBookingQueue</xsl:text>
</To>
<CorrelationId> 
</CorrelationId>
<ProcessId>
</ProcessId>   
<EventId>

</EventId>
<Domain>    
<xsl:text>Finance</xsl:text>
</Domain>   

</CMMHeader>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>


</xsl:stylesheet>

当前输出:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <tns:GLBookingMessage xmlns:tns="http://com.example/cdm/finance/generalledger/v1">
         <CMMHeader xmlns:cur="http://com.example/cdm/currencycodes/v1"
                    xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">
            <MessageTimeStamp>2014-11-14T05:06:44.</MessageTimeStamp>
            <MessageId/>
            <ComponentId>GLBookingFileAdapter</ComponentId>
            <From>Silta</From>
            <To>GLBookingQueue</To>
            <CorrelationId/>
            <ProcessId/>
            <EventId/>
            <Domain>Finance</Domain>
         </CMMHeader> 
         <tns:GLBooking> 
            <cdm:SubLedger xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">Payroll </cdm:SubLedger> 
            <cdm:Identifier xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">10004 </cdm:Identifier> 

            <cdm:GLBookingLine xmlns:cdm="http://com.example/cdm/finance/generalledger/v1"> 
               <cdm:LineNumber>1</cdm:LineNumber> 
               <cdm:AccountNumber>0000133200</cdm:AccountNumber> 
               <cdm:Description> </cdm:Description> 

            </cdm:GLBookingLine> 
            <cdm:GLBookingLine xmlns:cdm="http://com.example/cdm/finance/generalledger/v1"> 
               <cdm:LineNumber>2</cdm:LineNumber> 
               <cdm:AccountNumber>0000133205</cdm:AccountNumber> 
               <cdm:Description> </cdm:Description> 

            </cdm:GLBookingLine> 
         </tns:GLBooking> 
      </tns:GLBookingMessage>
   </soapenv:Body>
</soapenv:Envelope>

必填项:

<?xml version="1.0" encoding="utf-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
       <soapenv:Body>
          <tns:GLBookingMessage xmlns:tns="http://com.example/cdm/finance/generalledger/v1">
             <CMMHeader> 
                <MessageTimeStamp>2014-11-14T05:06:44.</MessageTimeStamp>
                <MessageId/>
                <ComponentId>GLBookingFileAdapter</ComponentId>
                <From>Silta</From>
                <To>GLBookingQueue</To>
                <CorrelationId/>
                <ProcessId/>
                <EventId/>
                <Domain>Finance</Domain>
             </CMMHeader> 
             <tns:GLBooking> 
                <cdm:SubLedger xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">Payroll </cdm:SubLedger> 
                <cdm:Identifier xmlns:cdm="http://com.example/cdm/finance/generalledger/v1">10004 </cdm:Identifier> 

                <cdm:GLBookingLine xmlns:cdm="http://com.example/cdm/finance/generalledger/v1"> 
                   <cdm:LineNumber>1</cdm:LineNumber> 
                   <cdm:AccountNumber>0000133200</cdm:AccountNumber> 
                   <cdm:Description> </cdm:Description> 

                </cdm:GLBookingLine> 
                <cdm:GLBookingLine xmlns:cdm="http://com.example/cdm/finance/generalledger/v1"> 
                   <cdm:LineNumber>2</cdm:LineNumber> 
                   <cdm:AccountNumber>0000133205</cdm:AccountNumber> 
                   <cdm:Description> </cdm:Description> 

                </cdm:GLBookingLine> 
             </tns:GLBooking> 
          </tns:GLBookingMessage>
       </soapenv:Body>
    </soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

我相信如果您将样式表标题更改为:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:tns="http://com.example/cdm/finance/generalledger/v1">

你可能得到你想要的东西。很难确定,因为 - 再一次(!) - 您的输出,无论是必需的还是声明的,都与您的输入不符。