2017-03-08 2 views
0

passiert ist:für das Bildelement wird die Transformation Meine XML-Eingabe nicht

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

XSL ich als:

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

Ich erhalte Ausgabe als:

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

Erwartete Ausgabe wäre:

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

Während der Verwendung des Bildes wird es als separates Thema angezeigt. Aber ich muss mit dem para-Tag innerhalb des h1-Themas kommen. Bitte geben Sie die Vorschlagscodes an. Vielen Dank im Voraus

Antwort

1

die Logik Unter der Annahme, dass, wenn ein p-Tag mit einem Kind img direkt vor einem h1 Tag erscheint, können Sie es tatsächlich wollen einen Teil dieser Gruppe sein, eher als Teil der vorhergehenden Gruppe, dann versuchen, diese 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> 
+0

Danke @TimC. Es funktioniert gut – User501

+0

Hi @TimC. Ich hatte eine ähnliche Frage. Weitere Informationen finden Sie unter http://stackoverflow.com/questions/42694705/for-the-paragraph-without-header-part-images-are-not-coming. Vielen Dank – User501

Verwandte Themen