2015-04-22 5 views
1

У меня есть следующий XML.Auto increment внутри шаблона

<?xml version="1.0" encoding="UTF-8"?> 
<uaeccc> 
    <nd> 
    <AN>-2-0001</AN> 
     <h2>Ttle2</h2> 
    </nd> 
    <nd> 
     <h1>Ttle1</h1> 
     <h2>Ttle2</h2> 
     <h3>Part 1 Contracts</h3> 
     <h4>par2 sub contractts</h4> 
    </nd> 
    <nd> 
     <h1>Ttle1</h1> 
     <h2>Ttle2 </h2> 
     <h3>Part 1 Contracts</h3> 
     <h4>Part 2 Sub contracts</h4> 
    </nd> 
    <nd> 
     <h1>Ttle1</h1> 
     <h2>Ttle2 </h2> 
     <h3>Part 1 Contracts</h3> 
     <h4>part 2 sub contracts</h4> 
    </nd> 
</uaeccc> 

Здесь я пытался увеличить число, но оно увеличивается только в этом разделе.

Ниже приведено мое XSLT.

<xsl:template match="uaeccc"> 
    <xsl:for-each select="nd"> 
     <xsl:apply-templates select="."/> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template name="section" match="nd"> 
    <xsl:apply-templates select="./node()[1][self::page]" mode="first"/> 
    <!-- Variables--> 
    <xsl:variable name="getChap"> 
     <xsl:value-of select="substring-before(substring-after((//AN)[1],'-'),'-')"/> 
    </xsl:variable> 
    <xsl:variable name="count"> 
     <xsl:number level="any"/> 
    </xsl:variable> 
    <xsl:variable name="classname"> 
     <!--Get name attribute of current node --> 
     <xsl:value-of select="concat('section-sect','1')"/> 
    </xsl:variable> 

    <xsl:variable name="Chn"> 
     <xsl:value-of select="format-number($getChap,'00')"/> 
    </xsl:variable> 
    <xsl:variable name="chapternumber"> 
     <xsl:value-of select="$Chn"/> 
    </xsl:variable> 
    <xsl:variable name="sectnum"> 
     <xsl:number level="any" count="section" format="1"/> 
    </xsl:variable> 
    <!--Create a string variable by concat string method --> 
    <xsl:variable name="sectionname"> 
     <xsl:value-of select="concat('CH_',$chapternumber,'-SEC-', $count)"/> 
    </xsl:variable> 
    <!-- Template Content --> 
    <div class="{$classname}"> 
     <a name="{$sectionname}"> </a> 
     <div class="section-title"> 
      <xsl:apply-templates select="h3"/> 
     </div> 
     <xsl:if test="./h4"> 
      <div class="{$classname}"> 
       <a name="{$sectionname}"> </a> 
       <div class="section-title2"> 
        <xsl:apply-templates select="h4"/> 
       </div> 
      </div> 
     </xsl:if> 
     <xsl:apply-templates select="child::node()[not(self::h4| self::h3)]"/> 
    </div> 
</xsl:template> 

<xsl:template match="AN"/> 

<xsl:template match="h1"/> 

<xsl:template match="h2"/> 

Мой выходной ток

<div class="section-sect1"> 
     <a name="CH_02-SEC-1"></a> 
     <div class="section-title"></div> 
    </div> 
    <div class="section-sect1"> 
     <a name="CH_02-SEC-2"></a> 
     <div class="section-title"> 
      <h3>Part 1 Contracts</h3> 
     </div> 
     <div class="section-sect1"> 
      <a name="CH_02-SEC-2"></a> 
      <div class="section-title2"> 
       <h4>par2 sub contractts</h4> 
      </div> 
     </div> 
    </div> 
    <div class="section-sect1"> 
     <a name="CH_02-SEC-3"></a> 
     <div class="section-title"> 
      <h3>Part 1 Contracts</h3> 
     </div> 
     <div class="section-sect1"> 
      <a name="CH_02-SEC-3"></a> 
      <div class="section-title2"> 
       <h4>Part 2 Sub contracts</h4> 
      </div> 
     </div> 
    </div> 
    <div class="section-sect1"> 
     <a name="CH_02-SEC-4"></a> 
     <div class="section-title"> 
      <h3>Part 1 Contracts</h3> 
     </div> 
     <div class="section-sect1"> 
      <a name="CH_02-SEC-4"></a> 
      <div class="section-title2"> 
       <h4>part 2 sub contracts</h4> 
      </div> 
     </div> 
    </div> 

и ожидаемый выход

<div class="section-sect1"> 
     <a name="CH_02-SEC-1"></a> 
     <div class="section-title"></div> 
    </div> 
    <div class="section-sect1"> 
     <a name="CH_02-SEC-2"></a> 
     <div class="section-title"> 
      <h3>Part 1 Contracts</h3> 
     </div> 
     <div class="section-sect1"> 
      <a name="CH_02-SEC-3"></a> 
      <div class="section-title2"> 
       <h4>par2 sub contractts</h4> 
      </div> 
     </div> 
    </div> 
    <div class="section-sect1"> 
     <a name="CH_02-SEC-4"></a> 
     <div class="section-title"> 
      <h3>Part 1 Contracts</h3> 
     </div> 
     <div class="section-sect1"> 
      <a name="CH_02-SEC-5"></a> 
      <div class="section-title2"> 
       <h4>Part 2 Sub contracts</h4> 
      </div> 
     </div> 
    </div> 
    <div class="section-sect1"> 
     <a name="CH_02-SEC-6"></a> 
     <div class="section-title"> 
      <h3>Part 1 Contracts</h3> 
     </div> 
     <div class="section-sect1"> 
      <a name="CH_02-SEC-7"></a> 
      <div class="section-title2"> 
       <h4>part 2 sub contracts</h4> 
      </div> 
     </div> 
    </div> 

Здесь я хочу, чтобы увеличить число после SEC- каждый раз, когда есть $classname called.But здесь, в моем XSLT , приращение происходит, но такое же и в случае подраздела.

, пожалуйста, дайте мне знать, как я могу исправить это.

Вот Working Demo

+0

Вот и совершенно неясно узнать, что вы будете делать и точно считать. Фактически '' '' * подсчет * "относительно' 'в настоящее время обрабатывается, так что вы не можете считать h1 h2 и так далее. Пожалуйста, объясните: 1) как вы хотите, чтобы ваш выходной документ нужно было организовать (похоже, вы хотите выполнить некоторую группировку) и 2) какие элементы должны использовать нумерации (кажется, вы хотите считать h3 и h4). Спасибо. – potame

+0

Привет @potame, Да, вы правы. здесь изначально я начал только путем подсчета '', позже я наткнулся на то, где, если есть 'h3' или' h4', счет должен быть основан на 'h3' или' h4'. – user3872094

+0

И, в конце концов, вы вообще не открываете 'h1' и' h2', не так ли? – potame

ответ

1

Я не могу сделать головы или хвосты кода. FWIW, следующая таблица стилей, при нанесении на вашем примере ввода, будет производить требуемую мощность (за исключением оберточной результат в виде одного корневого элемента):

XSLT 1,0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/uaeccc"> 
    <root> 
     <xsl:apply-templates/> 
    </root> 
</xsl:template> 

<xsl:template match="nd"> 
    <div class="section-sect1"> 
     <xsl:variable name="sectionnum"> 
      <xsl:number count="nd|h4" level="any"/> 
     </xsl:variable> 
     <a name="CH_02-SEC-{$sectionnum}"/> 
     <div class="section-title"> 
      <xsl:copy-of select="h3"/> 
     </div> 
     <xsl:apply-templates select="h4"/> 
    </div> 
</xsl:template> 

<xsl:template match="h4"> 
    <div class="section-sect1"> 
     <xsl:variable name="sectionnum"> 
      <xsl:number count="nd|h4" level="any"/> 
     </xsl:variable> 
     <a name="CH_02-SEC-{$sectionnum}"/> 
     <div class="section-title2"> 
      <xsl:copy-of select="."/> 
     </div> 
    </div> 
</xsl:template> 

</xsl:stylesheet> 
+0

Спасибо за решение :-) – user3872094