Thomas J.S.: Variable und String mit "xsl:if" vergleichen

Beitrag lesen

Hallo,

--------------- xml ----------------------

<?xml version="1.0" encoding="iso-8859-1"?>
<data xmlns:text="http://example.org/text">
 <text:p text:style-name="h1">h1 1</text:p>
 <text:p text:style-name="p">p 1</text:p>
 <text:p text:style-name="p">p 2</text:p>
 <text:p text:style-name="h1">h1 2</text:p>
 <text:p text:style-name="h2">h2 1</text:p>
 <text:p text:style-name="h3">h3 1</text:p>
 <text:p text:style-name="p">p 3</text:p>
 <text:p text:style-name="p">p 4</text:p>
 <text:p text:style-name="p">p 5</text:p>
 <text:p text:style-name="h2">h2 2</text:p>
 <text:p text:style-name="h3">h3 2</text:p>
 <text:p text:style-name="p">p 6</text:p>
 <text:p text:style-name="p">p 7</text:p>
 <text:p text:style-name="p">p 8</text:p>
 <text:p text:style-name="h2">h2 3</text:p>
 <text:p text:style-name="h3">h3 3</text:p>
 <text:p text:style-name="p">p 9</text:p>
 <text:p text:style-name="p">p 10</text:p>
 <text:p text:style-name="h1">h1 3</text:p>
 <text:p text:style-name="h2">h2 4</text:p>
 <text:p text:style-name="h3">h3 4</text:p>
 <text:p text:style-name="p">p 11</text:p>
 <text:p text:style-name="p">p 12</text:p>
 <text:p text:style-name="p">p 13</text:p>
</data>

--------------------- xsl ----------------
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:text="http://example.org/text">
 <xsl:template match="/data">
  <html>
   <head><title>Untitled</title></head>
   <body>
    <xsl:for-each select="//text:h | //text:p">
     xsl:choose
      <xsl:when test="@text:style-name='h1'">
       <xsl:if test="following-sibling::node()[1]/@text:style-name != 'h2'">
        <h1>
         <xsl:value-of select="."/>
        </h1>
       </xsl:if>
      </xsl:when>
      <xsl:when test="@text:style-name='h2'">
       <xsl:if test="preceding-sibling::node()[2]/@text:style-name != 'h1'">
        <h1>
         <xsl:value-of select="preceding-sibling::text:p[@text:style-name = 'h1'][1]"/>
        </h1>
       </xsl:if>
       <h2>
        <xsl:value-of select="."/>
       </h2>
      </xsl:when>
      <xsl:when test="@text:style-name='h3'">
       <h3>
        <xsl:value-of select="."/>
       </h3>
      </xsl:when>
      <xsl:when test="@text:style-name='p'">
       <p>
        <xsl:value-of select="." />
       </p>
      </xsl:when>
     </xsl:choose>
    </xsl:for-each>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

------------------------- ausgabe --------------
<html xmlns:text="http://example.org/text">
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-16">
<title>Untitled</title></head>
<body>
<h1>h1 1</h1>
<p>p 1</p>
<p>p 2</p>
<h1>h1 2</h1>
<h2>h2 1</h2>
<h3>h3 1</h3>
<p>p 3</p>
<p>p 4</p>
<p>p 5</p>
<h1>h1 2</h1>
<h2>h2 2</h2>
<h3>h3 2</h3>
<p>p 6</p>
<p>p 7</p>
<p>p 8</p>
<h1>h1 2</h1>
<h2>h2 3</h2>
<h3>h3 3</h3>
<p>p 9</p>
<p>p 10</p>
<h1>h1 3</h1>
<h2>h2 4</h2>
<h3>h3 4</h3>
<p>p 11</p>
<p>p 12</p>
<p>p 13</p>
</body>
</html>

Grüße
Thomas