2017-03-09 5 views
0
nicht

Ich habe die beide Header-Dateien (h1, h2), hatte ich die Bildinformationen unter Verwendung der unten XSL schreiben:für den Absatz ohne Kopfteil Bilder kommen

Mein Eingang XSL:

<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:value-of select="@src"/></xsl:attribute> 
      </xsl:if> 
     </xsl:element> 
    </xsl:template> 

Mein erster Eingang XML ist (das Bild für diese Art kommt nicht):

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

Meine zweite XML-Datei: (Sein Erzeugungs Bild korrekt)

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

Erzeugte Ausgabe für die erste XML:

<topic id="topic_1"> 
    <title>Taking</title> 
    <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> 
</body> 
    <topic id="topic_2"> 
     <title>Blood Pressure?</title> 
     <body> 
     <p>second.</p> 
     </body> 
    </topic> 
</topic> 

I XSL für beide Arten von Eingaben zu schaffen haben, Ob ​​Absatz oder nicht mit dem Tag mit . Bitte schlagen Sie die Codierung dafür vor. erste

Vielen Dank im Voraus

Antwort

0

Ich glaube, Sie brauchen eine gewisse Logik auf die Überprüfung der self:h2 so hinzuzufügen, dass, wenn sie auftritt in der ersten Position (dh er hat nicht p Tags, bevor es), dann gibt das dort Bild

<xsl:when test="self::h2"> 
    <xsl:if test="position() = 1"> 
     <body><xsl:apply-templates select="$image"/></body> 
    </xsl:if> 

Versuchen Sie, 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"> 
         <xsl:if test="position() = 1"> 
          <body><xsl:apply-templates select="$image"/></body> 
         </xsl:if> 
         <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:value-of select="@src"/></xsl:attribute> 
      </xsl:if> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

Dank @TimC. Es funktioniert gut. – User501

+0

Hallo @Timc. Bitte beachten Sie diese Frage http://stackoverflow.com/questions/42762246/the-header-image-needs-to-come-in-between-the-body – User501

Verwandte Themen