найти максимальную глубину главы - PullRequest
2 голосов
/ 12 мая 2011

каждый. В этом случае я хочу вычислить максимальную глубину главы. Например, книга без глав имеет высоту 0. В книге есть только главы без разделов, высота должна быть 1. Размер следующий: xml:

<book title="D">
<author>
  <name>abc</name>
</author>

<chapter title="chapter1">
  <section title="section1.1"/>
  <section title="section1.2">
    <section title="section1.2.1"/>
<section title="section1.2.2"/>
  </section>
  <section title="section1.3">
<section title="section1.3.1"/>
  </section>
</chapter>

<chapter title="chapter2"/>

</book>

Кстати, я использовал саксонский. Я хочу попробовать использовать только соответствующие шаблоны. В этом случае вывод - текст, а результат -

 3

Это мой XSL для вычисления глубины каждой ноты? Правильно ли это? И тогда как можно выставить максимум тока, вызвав шаблон с именем max ??

<xsl:transform version="2.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <xsl:apply-templates select="book"/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="book">
    <xsl:apply-templates select="chapter">
        <xsl:with-param name ="depth" select ="1"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="chapter|section">
    <xsl:param name="depth"  as="item()*"/>
    <xsl:variable name ="current" select ="$depth"/>
    <xsl:sequence select ="$depth"/>
    <xsl:if test ="not(empty(section))">
        <xsl:apply-templates select="section">
            <xsl:with-param name="depth" select="$depth+1"/>
        </xsl:apply-templates>

    </xsl:if>
 </xsl:template>
</xsl:transform >

Ответы [ 3 ]

2 голосов
/ 14 мая 2011

XSLT 1.0 решение :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:apply-templates select="//*[self::chapter or self::section]">
       <xsl:sort data-type="number" order="descending" select=
        "count(ancestor::*[self::chapter or self::section])
        "/>
     </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section">
    <xsl:if test="position()=1">
     <xsl:value-of select=
     "count(ancestor::*[self::chapter or self::section]) +1
     "/>
    </xsl:if>
 </xsl:template>
</xsl:stylesheet>

когда это преобразование применяется к предоставленному документу XML:

<book title="D">
    <author>
        <name>abc</name>
    </author>
    <chapter title="chapter1">
        <section title="section1.1"/>
        <section title="section1.2">
            <section title="section1.2.1"/>
            <section title="section1.2.2"/></section>
        <section title="section1.3">
            <section title="section1.3.1"/></section>
    </chapter>
    <chapter title="chapter2"/>
</book>

желаемый, правильный ответ выдается:

3
1 голос
/ 14 мая 2011

А вот и ваше решение - исправлено :

<xsl:transform version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output method="text" encoding="UTF-8"/>

 <xsl:template match="/">
  <xsl:variable name="vDepths" as="xs:integer*">
   <xsl:apply-templates select="book"/>
  </xsl:variable>

  <xsl:sequence select="max($vDepths)"/>
 </xsl:template>

 <xsl:template match="book" as="xs:integer*">
  <xsl:apply-templates select="chapter">
   <xsl:with-param name ="depth" select ="1"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="chapter|section" as="xs:integer*">
  <xsl:param name="depth"  as="xs:integer"/>
  <xsl:variable name ="current" select ="$depth"/>

  <xsl:sequence select ="$depth"/>
  <xsl:if test ="not(empty(section))">
   <xsl:apply-templates select="section">
    <xsl:with-param name="depth" select="$depth+1"/>
   </xsl:apply-templates>
  </xsl:if>
 </xsl:template>
</xsl:transform >
0 голосов
/ 14 мая 2011

Использование :

max(//(chapter|section)/count(ancestor::*[self::chapter or self::section]))+1

Это можно сделать несколько более эффективным :

max(//(chapter|section)[not(chapter or section)]
            /count(ancestor::*[self::chapter or self::section])
    ) +1
...