2016-06-12 16 views
0

Ich habe eine XML-Datei, die ich mit XSLT transformieren möchte. Die Idee ist, alles, was vor jeder <paragraph/> Tags in <p></p> Tags ist.kann nicht mit XPath auf Textblock zugreifen

XML-Datei:

<section> 
    Hello everyone, I'm 
    <bold>Hackmania</bold> 
    <bold>15</bold> 
    <line/> 
    I am looking for an 
    <highlight>answer</highlight> 
    <paragraph/> 

    Here is an other 
    <bold>paragraph</bold> 
    <highlight>with the same tags</highlight> 
    <paragraph/> 
</section> 

Gesucht transformiert XML:

<section> 
    <p> 
     Hello everyone, I'm 
     <bold>Hackmania</bold> 
     <bold>15</bold> 
     <line/> 
     I am looking for an 
     <highlight>answer</highlight> 
    </p> 
    <p> 
     HHere is an other 
     <bold>paragraph</bold> 
     <highlight>with the same tags</highlight> 
    </p> 
</section> 

Und hier ist meine XSL-Datei:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet> 
    <xsl:template match="/"> 
     <section> 
      <xsl:apply-templates/> 
     </section> 
    </xsl:template> 

    <xsl:template match="paragraph"> 
     <xsl:for-each select="."> 
      <p> 
       <xsl:apply-templates select="/*/*[preceding-sibling::paragraph]"/> 
      </p> 
     </xsl:for-each> 

    </xsl:template> 
</xsl:stylesheet> 

Vielen Dank im Voraus für die Ihnen helfen.

Antwort

1

ich tun würde:

XSLT 1,0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:key name="grpById" match="node()[not(self::paragraph)]" use="generate-id(following-sibling::paragraph[1])" /> 

<xsl:template match="/section"> 
    <xsl:copy> 
     <xsl:for-each select="paragraph"> 
      <p> 
       <xsl:copy-of select="key('grpById', generate-id())"/> 
      </p> 
     </xsl:for-each> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

Wenn Sie XSLT 2.0 verwenden können, dann:

XSLT 2.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/section"> 
    <xsl:copy> 
     <xsl:for-each-group select="node()" group-ending-with="paragraph"> 
      <p> 
       <xsl:copy-of select="current-group()[not(self::paragraph)]" /> 
      </p> 
     </xsl:for-each-group> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

danke für Ihre Hilfe @ michael.hor257k. – Hackmania15

0

versuchen, etwas wie folgt aus:

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="@*|node()"> 
      <xsl:copy> 
       <xsl:apply-templates select="node()"/> 
      </xsl:copy> 
    </xsl:template> 
    <xsl:template match="section"> 
     <xsl:apply-templates select="paragraph"/> 
    </xsl:template> 
    <xsl:template match="paragraph"> 
    <xsl:variable name="this" select="." /> 
    <p> 
    <xsl:apply-templates select="preceding-sibling::node()[not(self::paragraph) 
          and generate-id(following-sibling::paragraph) = 
           generate-id($this)]" /> 
    </p> 
    </xsl:template> 
</xsl:stylesheet> 

Aber die oben darüber im Klaren sein wird scheitern, wenn es nach dem letzten Absatz Inhalt. Zu handhaben, dass die Abschnittsvorlage ändern:

<xsl:template match="section"> 
     <xsl:apply-templates select="paragraph"/> 
     <xsl:apply-templates select="paragraph[last()]/following-sibling::node()"/> 
    </xsl:template> 
Verwandte Themen