简体   繁体   中英

For the Image element, the transformation is not happening

My Input xml is:

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

XSL I used as:

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

I'm getting output as:

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

Expected output would be:

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

While using the image, its coming as seperate topic. But i need to come by para tag inside the h1 topic. Please provide the suggestion codes. Thanks in advance

Assuming the logic is that if a p tag with a child img appears directly before an h1 tag, you actually want it to be part of that group, rather than part of the preceding group, then try this XSLT....

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Body">
        <xsl:for-each-group select="*" group-starting-with="h1">
           <xsl:if test="not(self::p/img)">
             <xsl:variable name="image" select="preceding-sibling::*[1][self::p/img]" />
             <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()[not(self::p/img)] except ."/></body>
                         </topic>
                      </xsl:when>
                      <xsl:otherwise>
                         <body><xsl:apply-templates select="$image|current-group()[not(self::p/img)]"/></body>
                      </xsl:otherwise>
                   </xsl:choose>
                </xsl:for-each-group>
                </topic>
             </xsl:if>
          </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>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM