2013-11-10 3 views
1

Я пытаюсь выяснить, как получить многопозиционное (1.1) узла при назначении ключам. Ниже урезанная воспроизведение выпуска:using xsl: number with xsl: key, чтобы получить позицию узла

Входной XML:

<doc> 
    <chapter> 
     <title>Chapter</title> 
     <section> 
      <para0> 
       <title>section</title> 
      </para0> 
     </section> 
     <section> 
      <para0> 
       <title>section</title> 
      </para0> 
     </section> 
     <procedure> 
      <title>procedure</title> 
     </procedure> 
     <section> 
      <para0> 
       <title>section</title> 
      </para0> 
     </section> 
     <section> 
      <para0> 
       <title>section</title> 
      </para0> 
     </section> 
     <procedure> 
      <title>procedure</title> 
     </procedure> 
    </chapter> 
</doc> 

Желаемая выход

<output> 
    <chapter>Chapter 1 
     <section>section 1.1</section> 
     <section>section 1.2</section> 
     <section>procedure 1.3</section> 
     <section>section 1.4</section> 
     <section>section 1.5</section> 
     <section>procedure 1.6</section> 
    </chapter> 
</output> 

выходной ток

<output> 
    <chapter>Chapter 1 
     <section>section 1</section> 
     <section>section 1</section> 
     <section>procedure 1</section> 
     <section>section 1</section> 
     <section>section 1</section> 
     <section>procedure 1</section> 
    </chapter> 
</output> 

Мой XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:key name="numbering" match="chapter/procedure" use="generate-id()"/> 
    <xsl:key name="numbering" match="chapter/section/para0" use="generate-id()" /> 
    <xsl:template match="/"> 
     <output> 
      <xsl:apply-templates/> 
     </output> 
    </xsl:template> 
    <xsl:template match="doc"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="chapter"> 
     <chapter> 
      <xsl:value-of select="title"/> 
      <xsl:text> </xsl:text> 
      <xsl:number level="multiple" count="chapter" format="1"/> 
      <xsl:for-each select="section/para0 | procedure "> 
       <section> 
        <xsl:value-of select="title"/> 
        <xsl:text> </xsl:text> 
        <xsl:number level="any" select="key('numbering', generate-id())/title" from="chapter" format="1.1"/> 
       </section> 
      </xsl:for-each> 
     </chapter> 
    </xsl:template> 

ответ

3

С Saxon 9.5 следующая таблица стилей производит вывод, который вы хотите:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/"> 
     <output> 
      <xsl:apply-templates/> 
     </output> 
    </xsl:template> 
    <xsl:template match="doc"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="chapter"> 
     <chapter> 
      <xsl:value-of select="title"/> 
      <xsl:text> </xsl:text> 
      <xsl:number level="multiple" count="chapter" format="1"/> 
      <xsl:apply-templates/> 
     </chapter> 
    </xsl:template> 

    <xsl:template match="section | procedure "> 
     <section> 
      <xsl:value-of select="(title, para0/title)[1]"/> 
      <xsl:text> </xsl:text> 
      <xsl:number level="multiple" count="chapter | section | procedure" format="1.1"/> 
     </section> 
    </xsl:template> 

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