2014-11-03 118 views
0

我有這樣的XML:XSL添加新元素名稱空間

<Row> 

<one>1</one> 
<two>2</two> 
<tree>3</tree> 
<four>4</four> 
<five>5</five> 

</Row> 

和IM希望得到的結果:

<n0:Result xmlns:ns0="http://www.my.schemas/schemas/event1.xsd" 
      xmlns:ns1="http://www.my.schemas/schemas/event2.xsd" 
      xmlns:ns2="http://www.my.schemas/schemas/event3.xsd"> 
    <n1:group1> 
     <n2:one>1</n2:one> 
     <n2:two>2</n2:two> 
    </n1:group1> 

    <n1:group2> 
     <n2:tree>3</n2:tree> 
     <n2:four>4</n2:four> 
    </n1:group2> 

    <n0:five>5</n0:five> 

</Result> 

我現在XSL是:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ns0="http://www.my.schemas/schemas/event1.xsd" 
      xmlns:ns1="http://www.my.schemas/schemas/event2.xsd" 
      xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" > 
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 


    <xsl:template match="Row"> 
    <result> 
       <group1> 
        <xsl:apply-templates select="one|two"/> 
       </group1> 
       <group2> 
        <xsl:apply-templates select="tree|four"/> 
       </group2> 

       <xsl:apply-templates select="five"/> 
    </result> 
    </xsl:template> 

</xsl:stylesheet> 

怎麼辦我將所需的名稱空間添加到新舊元素? 我不知道如何做到這一點與分組和新元素,只與現有的元素。

回答

2

簡單地寫那些元素,你從字面上寫了你想要的樣品中,從而

<xsl:template match="Row"> 
<result> 
      <group1> 
       <xsl:apply-templates select="one|two"/> 
      </group1> 
      <group2> 
       <xsl:apply-templates select="tree|four"/> 
      </group2> 

      <xsl:apply-templates select="five"/> 
</result> 
</xsl:template> 

成爲

<xsl:template match="Row"> 
<ns0:result> 
      <ns1:group1> 
       <xsl:apply-templates select="one|two"/> 
      </ns1:group1> 
      <ns1:group2> 
       <xsl:apply-templates select="tree|four"/> 
      </ns1:group2> 

      <xsl:apply-templates select="five"/> 
</ns0:result> 
</xsl:template> 

很明顯,那麼你需要確保其他模板還命名空間中的輸出它們的元素你希望他們屬於。

一個更完整的例子是

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ns0="http://www.my.schemas/schemas/event1.xsd" 
      xmlns:ns1="http://www.my.schemas/schemas/event2.xsd" 
      xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" > 
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/> 

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


<xsl:template match="Row"> 
<ns0:result> 
      <ns1:group1> 
       <xsl:apply-templates select="one|two"/> 
      </ns1:group1> 
      <ns1:group2> 
       <xsl:apply-templates select="tree|four"/> 
      </ns1:group2> 

      <xsl:apply-templates select="five"/> 
</ns0:result> 
</xsl:template> 

</xsl:stylesheet> 

然後,正如我所說,如果你變換等元素,你將要添加模板,所以你還需要

​​

<xsl:template match="three | four"> 
    <xsl:element name="ns2:{local-name()}"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

and

<xsl:template match="five"> 
    <xsl:element name="ns0:{local-name()}"> 
    <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

這麼幹脆就是

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:ns0="http://www.my.schemas/schemas/event1.xsd" 
      xmlns:ns1="http://www.my.schemas/schemas/event2.xsd" 
      xmlns:ns2="http://www.my.schemas/schemas/event3.xsd" > 
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/> 

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


<xsl:template match="Row"> 
<ns0:result> 
      <ns1:group1> 
       <xsl:apply-templates select="one|two"/> 
      </ns1:group1> 
      <ns1:group2> 
       <xsl:apply-templates select="tree|four"/> 
      </ns1:group2> 

      <xsl:apply-templates select="five"/> 
</ns0:result> 
</xsl:template> 

    <xsl:template match="one | two"> 
     <xsl:element name="ns1:{local-name()}"> 
     <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 



    <xsl:template match="three | four"> 
     <xsl:element name="ns2:{local-name()}"> 
     <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 



    <xsl:template match="five"> 
     <xsl:element name="ns0:{local-name()}"> 
     <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 


</xsl:stylesheet> 
+0

我試過了,但如果你只是寫N0:導致XSL,你會得到結果和NO N0 – lshaked 2014-11-03 09:14:11

+0

@Ishaked,我已經編輯了答案,並試圖展示如何來轉換其他元素,使它們最終進入名稱空間。 – 2014-11-03 09:27:24

+0

太好了,謝謝!工作:) – lshaked 2014-11-03 09:39:37