2014-11-03 3 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="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> 

Так в целом, что является

<?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

+0

@Ishaked, я редактировал ответ и попытался показать, как для преобразования других элементов, чтобы они попадали в пространство имен. –

+0

Отлично, спасибо! работает :) – lshaked

Смежные вопросы