【问题标题】:Remove namespaces from an element using XSLT 2.0使用 XSLT 2.0 从元素中删除名称空间
【发布时间】:2014-11-15 04:27:31
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-13
      • 2015-10-05
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 2019-11-26
      相关资源
      最近更新 更多