转换XSL时图像元素正在跳过

时间:2017-12-04 09:36:42

标签: xml xslt xslt-2.0

我正在使用image元素,在转换XSL Tranformation时跳过它,请更改我提供的代码。因为这段代码适用于很多输入文件,

我的输入XML:

<Body>
<h1>Local</h1>
<p>Early</p>
<h2>Home</h2>
<h3>Medicines</h3>
<p>Home</p>
<h3>General</h3>
<p>
<img alt="Man" height="187" width="300" class="right" src="https://tneb.com" />
</p>
<ul>
<li>
<p>Itching</p>
</li>
<li>
<p>pack</p>
</li>
</ul>
</Body>

我使用的XSLT 2.0和我使用saxon转换 - PE 9.6.0.7,

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

            <xsl:for-each-group select="current-group() except ." group-starting-with="h2 | div[@data-swiq-type='callout-box']">
               <xsl:choose>
                  <xsl:when test="self::h2">
                     <topic outputclass="TOPIC-Section">
                        <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                        <title outputclass="Section">
                           <xsl:apply-templates select="node()"/>
                        </title>

                        <xsl:for-each-group select="current-group() except ." group-starting-with="h3 | div[@data-swiq-type='callout-box']">
                           <xsl:choose>
                              <xsl:when test="self::h3">
                                 <topic outputclass="TOPIC-Sub-Section">
                                    <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                                    <title outputclass="Sub-Section">
                                       <xsl:apply-templates select="node()"/>
                                    </title>

                                    <xsl:for-each-group select="current-group() except ." group-starting-with="h4 | div[@data-swiq-type='callout-box']">
                                       <xsl:choose>
                                          <xsl:when test="self::h4">
                                             <topic outputclass="TOPIC-Sub-Section">
                                                <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                                                <title outputclass="Sub-Section">
                                                   <xsl:apply-templates select="node()"/>
                                                </title>

                                                <xsl:for-each-group select="current-group() except ." group-starting-with="h5 | div[@data-swiq-type='callout-box']">
                                                   <xsl:choose>
                                                      <xsl:when test="self::h5">
                                                         <topic outputclass="TOPIC-Sub-Section">
                                                            <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                                                            <title outputclass="Sub-Section">
                                                               <xsl:apply-templates select="node()"/>
                                                            </title>


                                                            <body><xsl:apply-templates select="current-group() except ."/></body>
                                                         </topic>
                                                      </xsl:when>
                                                      <xsl:when test="self::div[@data-swiq-type='callout-box']">
                                                         <topic outputclass="TOPIC-Box">
                                                            <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                                                            <title><xsl:value-of select="h5"/></title>
                                                            <body><xsl:apply-templates/></body>
                                                         </topic>
                                                      </xsl:when>
                                                      <xsl:otherwise>
                                                         <body><xsl:apply-templates select="current-group()"/></body>
                                                      </xsl:otherwise>
                                                   </xsl:choose>
                                                </xsl:for-each-group>

                                                </topic>
                                          </xsl:when>
                                          <xsl:when test="self::div[@data-swiq-type='callout-box']">
                                             <topic outputclass="TOPIC-Box">
                                                <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                                                <title><xsl:value-of select="h4"/></title>
                                                <body><xsl:apply-templates/></body>
                                             </topic>
                                          </xsl:when>
                                          <xsl:otherwise>
                                             <body><xsl:apply-templates select="current-group()"/></body>
                                          </xsl:otherwise>
                                       </xsl:choose>

                                    </xsl:for-each-group>

                                 </topic>                      
                              </xsl:when>
                              <xsl:when test="self::div[@data-swiq-type='callout-box']">
                                 <topic outputclass="TOPIC-Box">
                                    <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                                    <title><xsl:value-of select="h3"/></title>
                                    <body><xsl:apply-templates/></body>
                                 </topic>
                              </xsl:when>
                              <xsl:otherwise>
                                 <body><xsl:apply-templates select="current-group()"/></body>
                              </xsl:otherwise>
                           </xsl:choose>

                        </xsl:for-each-group>
                     </topic>
                  </xsl:when>
                  <xsl:when test="self::div[@data-swiq-type='callout-box']">
                     <topic outputclass="TOPIC-Box">
                        <xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4 | h5 | div"/></xsl:attribute>
                        <title outputclass=""><xsl:value-of select="h2 | h3 | h4 | h5"/></title>
                        <body><xsl:apply-templates/></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">
      <xsl:choose>
         <xsl:when test="not(text()[normalize-space(.)] | child::*)"/>
         <xsl:otherwise>
            <p outputclass="Body_text_no_indent">
               <xsl:apply-templates/>
            </p>            
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

      <xsl:template match="img">
      <xsl:element name="image">
            <xsl:if test="@imageid">
               <xsl:attribute name="id"><xsl:value-of select="@imageid"/></xsl:attribute>
            </xsl:if>
            <xsl:if test="@title">
               <xsl:attribute name="xtrf"><xsl:apply-templates select="@title"/></xsl:attribute>
             </xsl:if>
            <xsl:if test="@src">
               <xsl:attribute name="href">
                  <xsl:variable name="resolutionremove">
                  <xsl:value-of select="tokenize(@src, '/')[position() = last() - 2 or position() = last()]" separator="_"/>
                  </xsl:variable>
                  <xsl:value-of select="replace($resolutionremove, '.([0-9]+)x([0-9]+)', '')"/>
               </xsl:attribute>
          </xsl:if>
            <xsl:if test="@alt">
            <xsl:attribute name="alt"><xsl:apply-templates select="@alt"/></xsl:attribute>
            </xsl:if>
         <xsl:if test="@height">
            <xsl:attribute name="height"><xsl:value-of select="@height"/></xsl:attribute>
         </xsl:if>
         <xsl:if test="@width">
            <xsl:attribute name="width"><xsl:value-of select="@width"/></xsl:attribute>
         </xsl:if>
         <xsl:if test="@align">
            <xsl:attribute name="align"><xsl:value-of select="@align"/></xsl:attribute>
         </xsl:if>
            <xsl:if test="@data-image">
               <xsl:attribute name="base">data-image(<xsl:value-of select="@data-image"/>)</xsl:attribute>
            </xsl:if>            
         <xsl:attribute name="outputclass">Image_Ref</xsl:attribute>
      </xsl:element>
   </xsl:template>

<xsl:template match="ul" priority="0">
      <ul outputclass="">
         <xsl:apply-templates/>
      </ul>
   </xsl:template>

   <xsl:template match="ul/li" priority="0">
      <li outputclass="List_Bullet">
         <xsl:apply-templates/>
         <xsl:apply-templates select="following-sibling::*[1][self::ul]" mode="wrap"/>
      </li>
   </xsl:template>

当我转换时,图像元素正在跳过,如果我在list标签中使用这个img意味着它的到来。任何人都可以请给我这个建议。请在Body模板的提供代码中进行更改。提前致谢

1 个答案:

答案 0 :(得分:3)

更改

<xsl:for-each-group select="*[normalize-space()]" group-starting-with="h1">

<xsl:for-each-group select="*[node()]" group-starting-with="h1">