2015-07-24 8 views
0

У меня есть XML, как следующий XML,XSLT - добавить новый узел в данное положение

<doc> 
<footnote> 
    <p type="Foot"> 
     <link ref="http://www.facebook.com"/> 
     <c>content</c> 
     <d>cnotent</d> 
    </p> 
    <p type="Foot"> 
     <link ref="http://www.google.com"/> 
     <c>content</c> 
     <d>cnotent</d> 
    </p> 
</footnote> 
</doc> 

Мои requirmets являются

1) добавить динамический идентификатор <p> узла, который имеет атрибут type="Foot"

2) добавить новый узел с именем <newNode> внутри <p> узел

3) добавить динамический идентификатор к < newNode>

так выход sholud быть

<doc> 
<footnote> 
    <p id="ref-1" type="Foot"> 
     <newNode type="direct" refId="foot-1"/> 
     <link ref="http://www.facebook.com"/> 
     <c>content</c> 
     <d>cntent</d> 
    </p> 
    <p id="ref-2" type="Foot"> 
     <newNode type="direct" refId="foot-2"/> 
     <link ref="http://www.google.com"/> 
     <c>content</c> 
     <d>cotent</d> 
    </p> 
</footnote> 
</doc> 

Я написал следующее XSL, чтобы сделать это

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

    <!-- add new dynamic attribute to <p> --> 
    <xsl:template match="p[@type='Foot']"> 
     <xsl:copy> 
      <xsl:attribute name="id"> 
       <xsl:value-of select="'ref-'"/> 
       <xsl:number count="p[@type='Foot']" level="any"/> 
      </xsl:attribute> 

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

      <!-- add new node with dynamic attribute --> 
      <newNode type="direct"> 
       <xsl:attribute name="refId"> 
        <xsl:value-of select="'foot-'"/> 
        <xsl:number count="p[@type='Foot']" level="any"></xsl:number> 
       </xsl:attribute> 
      </newNode> 
     </xsl:copy> 

    </xsl:template> 

Моя проблема в том, что добавление нового узла объявление последний узел внутри <p> узла (как показано ниже), который я необходимо добавить в качестве первого узла внутри <p> узел

Как разместить первый узел внутри узла <p>, как показано ниже?

<p id="ref-1" type="Foot"> 
     <newNode type="direct" refId="foot-1"/> 
     <link ref="http://www.facebook.com"/> 
     <c>content</c> 
     <d>cntent</d> 
    </p> 

ответ

1

Вы должны убедиться, что вы копируете дочерние элементы после создания нового узла:

<xsl:template match="p[@type='Foot']"> 
    <xsl:copy> 
     <xsl:attribute name="id"> 
      <xsl:value-of select="'ref-'"/> 
      <xsl:number count="p[@type='Foot']" level="any"/> 
     </xsl:attribute> 

     <xsl:apply-templates select="@*" /> 

     <!-- add new node with dynamic attribute --> 
     <newNode type="direct"> 
      <xsl:attribute name="refId"> 
       <xsl:value-of select="'foot-'"/> 
       <xsl:number count="p[@type='Foot']" level="any"></xsl:number> 
      </xsl:attribute> 
     </newNode> 

     <xsl:apply-templates/> 
    </xsl:copy> 

</xsl:template> 
Смежные вопросы