как извлечь дочерние элементы из вложенного тега в xslt - PullRequest
0 голосов
/ 02 марта 2019

У меня есть сценарий в xml:

<body>
<div><i>italic</i></div>
<div  id="123">
    <h1>Example</h1>
    <div  id="1234">
        <h1>heading 1</h1>
        <p>computer</p>
        <div>
            <i>italic 2</i>
        </div>    
    </div>
    <div  id="12345">
        <h1>heading 1</h1>
    </div>
</div>

Мне нужно применить правило, которое div преобразуется в раздел, и div, в котором значение h1 равно Пример, удалите этот тег h1и добавьте атрибут class = в тег раздела.

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

<body>
    <section>
        <i>italic<i>
    </section>
    <section class="example">
        <title>heading 1</title>
        <p>computer</p>
    </section>
    <section>
        <i>italic 2</i>
    </section>
    <section >
        <title>heading 1</title>
    </section>
</body>

my xslt:

<xsl:template match="//div">
    <xsl:choose>
        <xsl:when test="div">
            <xsl:apply-templates />
        </xsl:when>
        <xsl:otherwise>
            <section>
                <xsl:apply-templates />
            </section>
        </xsl:otherwise>
     </xsl:choose>
 </xsl:template>
<xsl:template match="div[h1 = 'Example']/h1" />
   <xsl:template match="div[h1 = 'Example']">
      <xsl:copy>
         <xsl:apply-templates select="./node()" />
      </xsl:copy>
   </xsl:template>
   <xsl:template match="div[h1 = 'example']">
   <section>
      <xsl:attribute name="class">
         <xsl:value-of select="'example'" />
      </xsl:attribute>
      <xsl:apply-templates />
      </section>
   </xsl:template>

фактический вывод:

<section class="example">
    <section>
        <title>heading 1</title>
        <p>computer</p>
        <section>
            <i>italic 2</i>
        </section>
     </section>
     <section >
         <title>heading 1</title>
     </section>
</section>

Не могли бы вы предложить, что мне делать здесь, чтобы получить ожидаемый результат.

...