2013-10-10 1 views
1

У меня этот XML как мой вход:XSLT: Как изменить имя тега, если это родительский или дочерний узел?

 <unidad num="2."> 
       <tag></tag> 
       <tag2></tag2> 
     <unidad num="2.1"> 
        <tag></tag> 
        <tag2></tag2>   
        <unidad num="2.1.1"> 
        <tag></tag> 
        <tag2></tag2> 
        <unidad num="2.1.1.1"> 
         <tag></tag> 
         <tag2></tag2> 
        </unidad> 
        </unidad> 
       </unidad> 
      </unidad> 

Мой вывод должен быть:

 <sub> 
       <tag></tag> 
       <tag2></tag2> 
     <sub2> 
        <tag></tag> 
        <tag2></tag2>   
        <sub3> 
        <tag></tag> 
        <tag2></tag2> 
        <sub4> 
         <tag></tag> 
         <tag2></tag2> 
        </sub4> 
        </sub3> 
       </sub2> 
      </sub> 

Я не могу найти правильный способ сделать это. Я работаю с шаблонами, я имею это:

<xsl:for-each select="unidad"> 
     <xsl:call-template name="unidades1"/> 
    </xsl:for-each> 

     <xsl:template name="unidades1"> 
    <xsl:element name="sub1"> 
     <xsl:text></xsl:text> 
      </xsl:element> 
      <xsl:if test="position() != last()"> 
     <xsl:apply-templates select="child::*"/> 
    </xsl:if> 
     </xsl:template> 

     <xsl:template match="unidad"> 
      <xsl:call-template name="unidades2"/> 
     </xsl:template> 


     <xsl:template name="unidades2"> 
    <xsl:element name="sub2"> 
     <xsl:text></xsl:text> 
      </xsl:element> 
      <xsl:if test="position() != last()"> 
     <xsl:apply-templates select="child::*"/> 
    </xsl:if> 
     </xsl:template> 

С помощью этого XSLT, каждый ребенок Унидад соответствует второму условию, так написано, как sub2, и я не знаю, как считать, если ему является дочерним элементом другого элемента unidad. Любые идеи, как достичь этого? Благодаря!

ответ

0

Эта таблица стилей дает желаемый результат. Он использует модифицированный identity transform со специальным шаблоном для элементов <unidad>.

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

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

    <xsl:template match="unidad"> 
<!--count the number of ancestors that are unidad elements--> 
     <xsl:variable name="unidad-ancestor-count" select="count(ancestor::unidad)"/> 

<!--If there are at least one unidad ancestors then 
    set the suffix to be the count()+1--> 
     <xsl:variable name="suffix"> 
      <xsl:if test="$unidad-ancestor-count>0"> 
       <xsl:value-of select="$unidad-ancestor-count+1"/> 
      </xsl:if> 
     </xsl:variable> 

<!--create a new element using a base name of "sub" and the suffix value --> 
     <xsl:element name="sub{$suffix}"> 
<!--not pushing the @num attribute through the identity template, 
    just descendant nodes--> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 

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