2017-02-27 11 views
0

Ich möchte die Topic-Elemente mit dem XSL bestellen. Ich habe viel Segment, das alle Segmente, die ich als separates Thema genommen hatte. Jetzt möchte ich die verschiedenen Eingabe bekommen, so möchte ich, wie sich ändern untenMüssen die Topic-Ausrichtung ändern

Meine Eingangs XML ist:

<Segments> 
<Segment> 
<Name>Food poisoning?</Name> 
<Body> 
<p>Food</p> 
</Body> 
</Segment> 
<Segment> 
<Name>Causes food poisoning?</Name> 
<Body> 
<p>Most</p> 
<h3>Salmonella</h3> 
<p>most</p> 
</Body> 
</Segment> 
</Segments> 

XSL ich als:

<xsl:template match="Segments"> 
<xsl:for-each-group select="*" group-starting-with="Segments"> 
<topic outputclass=""> 
<xsl:attribute name="id">topic_Head_<xsl:number count="Segments | Segment"/></xsl:attribute> 
<title/> 

<xsl:for-each-group select="current-group()" group-starting-with="Segment"> 
<xsl:choose> 
<xsl:when test="self::Segment"> 
<topic> 
<xsl:attribute name="id">topic_<xsl:number count="Segment"/></xsl:attribute> 
<xsl:apply-templates select="node()"/> 
</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="Name"> 
<title><xsl:apply-templates/></title> 
</xsl:template> 

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

<xsl:template match="h3"> 
</xsl:template>   

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

Output Ich erhalte als:

<topic outputclass="" id="topic_Head_1"> 
<title/> 
<topic id="topic_1"> 
<title>Food poisoning?</title> 
<body> 
<p>Food</p> 
</body> 
</topic> 
<topic id="topic_2"> 
<title>Causes food poisoning?</title> 
<body> 
<p>Most</p> 

<p>most</p> 
</body> 
</topic> 
</topic> 

Erwartete Ausgabe wie:

<topic outputclass="" id="topic_Head_1"> 
<title/> 
<topic id="topic_1"> 
<title>Food poisoning?</title> 
<body> 
<p>Food</p> 
</body> 
</topic> 
<topic id="topic_2"> 
<title>Causes food poisoning?</title> 
<body> 
<p>Most</p> 
</body> 
<topic id="topic_3"> 
<title>Salmonella</title> 
<body> 
<p>most</p> 
</body> 
</topic> 
</topic> 
</topic> 

Ich brauche das h3-Tag muss wie ein anderes Unterthema innerhalb des Segments kommen. Bitte geben Sie Ihre Vorschläge an. Vielen Dank im Voraus

Antwort

1

Meine Idee ist: für das Body-Element thake 2 verschiedene Wege, ob oder nicht h3 auftritt. Kein h3 Pfad ist trivial. Für h3 Pfad:

  • ersten Prozess alle Elemente vor dem ersten h3
  • Gruppe alle anderen Elemente, mit h3 Start Thema Tag für eatch Gruppe erstellen und anwenden Vorlagen

Hier ist der Code

<xsl:template match="Body"> 
<xsl:choose> 
    <xsl:when test="h3"> 
     <body> 
     <xsl:apply-templates select="h3[1]/preceding-sibling::*"/> 
     </body>      
     <xsl:for-each-group select="h3[1] | h3[1]/following-sibling::*" group-starting-with="h3"> 
     <topic> 
      <xsl:attribute name="id">topic_<xsl:number count="Segment | h3" level="any"/></xsl:attribute> 
      <title><xsl:value-of select="." /></title> 
      <xsl:apply-templates select="current-group()"/> 
     </topic> 
     </xsl:for-each-group>  
    </xsl:when> 
    <xsl:otherwise> 
     <body> 
     <xsl:apply-templates select="node()"/> 
     </body> 
    </xsl:otherwise> 
</xsl:choose> 
</xsl:template> 
+0

Es funktioniert gut @Lesiak. Aber für den ersten h3 Titel havingh der Para-Inhalt wie am meisten. Anders als für die restlichen h3 Tiltes funktionieren gut. Bitte überprüfen Sie – User501

+0

Bitte überprüfen Sie diese XSL Transform Link http://xsltransform.net/94AbWAY/1 – User501

+0

Aktualisierte Antwort mit "h3 [1] | h3 [1]/folgende Geschwister :: *" (hinzugefügt h3 [1]) in thew vorherige Antwort Ich bearbeitete vorhergehende und folgende Geschwister, und vermied das h3 elem. Du könntest dir vorstellen, dass du einfach so etwas wie: * [nicht (h3 [1]/folgendes Geschwister :: *)] – Lesiak

Verwandte Themen