如何从xslt中的嵌套标记中提取子元素

时间:2019-03-02 06:59:17

标签: xml xslt-1.0

我在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转换为section的规则以及h1值为Example的div,删除h1标记并将属性class =添加到section标记。

预期输出:

<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>

我的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>

请问我在这里应该怎么做才能获得预期的输出。

0 个答案:

没有答案
相关问题