2012-03-19 52 views
1

我有一個XML文件,必須使用XSLT進行轉換,我將一個節點添加到子樹中。 輸入文件如下:XSLT轉換生成一個空的名稱空間

<?xml version="1.0" encoding="UTF-8"?> 
    <Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<Context> 
    <ContextExtra> 
     <ns3:timestampList xmlns:ns3="http://www.mydomain.com/myAdapter/basicMessage/1.0" 
          xmlns:ns2="http://www.mydomain.com/myAdapter/tbMessage/1.0" 
          xmlns="http://www.mydomain.com/myAdapter/timestamp/1.0" 
          xmlns:ns10="http://www.mydomain.com/myAdapter/coMessage/1.0" 
          xmlns:ns11="http://www.mydomain.com/myAdapter/lolMessage/1.0" 
          xmlns:ns12="http://www.mydomain.com/myAdapter/tcMessage/1.0" 
          xmlns:ns13="http://www.mydomain.com/myAdapter/bMessage/1.0" 
          xmlns:ns4="http://www.mydomain.com/myAdapter/tiMessage/1.0" 
          xmlns:ns5="http://www.myDomain.com/myAdapter/oriMessage/1.0" 
          xmlns:ns6="http://www.myDomain.com/myAdapter/eiMessage/1.0" 
          xmlns:ns7="http://www.myDomain.com/myAdapter/fplscMessage/1.0" 
          xmlns:ns8="http://www.myDomain.com/myAdapter/psMessage/1.0" 
          xmlns:ns9="http://www.myDomain.com/myAdapter/stMessage/1.0"> 
      <timestampInfo> 
       <timestampID>START</timestampID> 
       <timestamp>2012-02-25T00:30:18.705+01:00</timestamp> 
       <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </timestampInfo> 
      <timestampInfo> 
       <timestampID>END</timestampID> 
       <timestamp>2012-02-25T00:30:23.675+01:00</timestamp> 
       <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </timestampInfo> 
     </ns3:timestampList> 

    </ContextExtra> 
</Context> 

轉型看起來是這樣的

<?xml version="1.0" encoding="UTF-8" ?> 

<xsl:output method="xml" indent="yes" />  

<xsl:param name="newTimeStamp"> 
    <timestampInfo> 
     <timestampID> 
      <xsl:text>TEST TEST</xsl:text> 
     </timestampID> 
     <timestamp> 
      <xsl:text>2012-02-25T00:30:23.654+01:00</xsl:text> 
     </timestamp> 
     <description> 
      <xsl:text>this is a test</xsl:text> 
     </description> 
    </timestampInfo> 
</xsl:param> 

<xsl:template match="//ts:timestampInfo[position()=last()]"> 
    <xsl:call-template name="identity" /> 
    <xsl:copy-of select="$newTimeStamp" /> 
</xsl:template> 

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

和RESU LT如下:

<?xml version="1.0" encoding="UTF-8"?> 
<Envelope> 
<Context> 
    <ContextExtra> 
     <ns3:timestampList xmlns="http://www.myDomain.com/myAdapter/timestamp/1.0" xmlns:ns3="http://www.myDomain.com/myAdapter/basicMessage/1.0"> 
      <timestampInfo> 
       <timestampID>MTA_START</timestampID> 
       <timestamp>2012-02-25T00:30:18.705+01:00</timestamp> 
       <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </timestampInfo> 
      <timestampInfo> 
       <timestampID>MTA_END</timestampID> 
       <timestamp>2012-02-25T00:30:23.675+01:00</timestamp> 
       <description xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> 
      </timestampInfo> 
      <timestampInfo xmlns=""> 
       <timestampID>TEST TEST</timestampID> 
       <timestamp>2012-02-25T00:30:23.654+01:00</timestamp> 
       <description>This is a test</description> 
      </timestampInfo> 
     </ns3:timestampList> 
    </ContextExtra> 
</Context> 

正如你可以看到timestampInfo節點必須爲屬性xmlns=""。也就是說,它是一個空的名字空間。而不是<timestampInfo xmlns="">我想有節點的形式<timestampInfo>。輸入文件具有這種格式,我無法控制它。

我該如何做到這一點?

在此先感謝

回答

0

指定timestampInfo要添加的命名空間:

<xsl:param name="newTimeStamp"> 
    <timestampInfo xmlns="http://www.myDomain.com/myAdapter/timestamp/1.0"> 
     <timestampID> 
      <xsl:text>TEST TEST</xsl:text> 
     </timestampID> 
     <timestamp> 
      <xsl:text>2012-02-25T00:30:23.654+01:00</xsl:text> 
     </timestamp> 
     <description> 
      <xsl:text>this is a test</xsl:text> 
     </description> 
    </timestampInfo> 
</xsl:param> 
0

只記得序列化輸出存在該命名空間的聲明。在你的轉換產生的結果樹中,重要的不是什麼名稱空間聲明存在(因爲它們不存在),而是每個元素的名稱是什麼?轉換的問題在於timestampInfo元素的名稱錯誤 - 更具體地說,它們具有正確的本地名稱,但名稱空間錯誤。獲取正確的元素名稱,序列化程序將負責生成名稱空間聲明。當然,當您使用文字結果元素時,結果樹中元素的名稱與樣式表中元素的名稱(名稱空間和全部)相同。