对于Image元素,转换不会发生

时间:2017-03-08 11:31:18

标签: xml xslt xslt-2.0

我的输入xml是:

<Body>
<p><img src="https://tneb.com"/></p>
<h1>Taking</h1>
<p>Your records.</p>
<h2>Blood Pressure?</h2>
<p>second.</p>
</Body>

XSL我用作:

    <xsl:template match="Body">
          <xsl:for-each-group select="*" group-starting-with="h1">
             <topic>
                <xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
                <title>
                   <xsl:apply-templates select="node()"/>
                </title>

                <xsl:for-each-group select="current-group() except ." group-starting-with="h2">
                   <xsl:choose>
                      <xsl:when test="self::h2">
                         <topic>
                            <xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
                            <title>
                               <xsl:apply-templates select="node()"/>
                            </title>
                            <body><xsl:apply-templates select="current-group() except ."/></body>
                         </topic>
                      </xsl:when>
                      <xsl:otherwise>
                         <body><xsl:apply-templates select="current-group()"/></body>
                      </xsl:otherwise>
                   </xsl:choose>
                </xsl:for-each-group>
                </topic>
          </xsl:for-each-group>
       </xsl:template>

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

       <xsl:template match="img">
      <xsl:element name="image">
            <xsl:if test="@src">
               <xsl:attribute name="href"><xsl:apply-templates select="@src"/></xsl:attribute>
            </xsl:if>
       </xsl:element>
   </xsl:template>

我的输出为:

<topic id="topic_">
   <title>
      <image href="https://tneb.com"/>
   </title>
</topic>
<topic id="topic_1">
   <title>Taking</title>
   <body>
      <p>Your records.</p>
   </body>
   <topic id="topic_2">
      <title>Blood Pressure?</title>
      <body>
         <p>second.</p>
      </body>
   </topic>
</topic>

预期输出为:

<topic id="topic_1">
   <title>Taking</title>
   <body>
      <p><image href="https://tneb.com"/></p>
      <p>Your records.</p>
   </body>
   <topic id="topic_2">
      <title>Blood Pressure?</title>
      <body>
         <p>second.</p>
      </body>
   </topic>
</topic>

在使用图片时,它将作为单独的主题出现。但我需要在h1主题中使用para标签。请提供建议代码。提前致谢

1 个答案:

答案 0 :(得分:1)

假设逻辑是如果带有子p的{​​{1}}标记直接出现在img标记之前,您实际上希望它成为该组的一部分,而不是前面的组,然后尝试这个XSLT ....

h1
相关问题