Help needed with transition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hvid Hat

    Help needed with transition

    Hi

    Can anyone help me with the following transition? My problem is how to create
    a fieldset each time I run into a heading and then include the following text
    elements within the fieldset.

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <field>
    <heading>Firs t heading</heading>
    </field>
    <field>
    <text>Lorem</text>
    </field>
    <field>
    <text>Ipsum</text>
    </field>
    <field>
    <heading>Seco nd heading</heading>
    </field>
    <field>
    <text>Dolor</text>
    </field>
    <field>
    <text>Sit</text>
    </field>
    <field>
    <heading>Thir d heading</heading>
    </field>
    <field>
    <text>Amet</text>
    </field>

    To

    <fieldset>
    <legend>First heading</legend>
    <p>Lorem</p>
    <p>Ipsum</p>
    </fieldset>
    <fieldset>
    <legend>Secon d heading</legend>
    <p>Dolor</p>
    <p>Sit</p>
    </fieldset>
    <fieldset>
    <legend>Third heading</legend>
    <p>Amet</p>
    </fieldset>
  • Hvid Hat

    #2
    Re: Help needed with transition

    I'm getting closer but still no cigar :-(

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:styleshe et version="1.0"
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"<xsl: template match="/">
    <xsl:apply-templates select="//heading" />
    </xsl:template>
    <xsl:template match="heading" >
    <fieldset>
    <legend>
    <xsl:value-of select="."/>
    </legend>
    <!-- Only text elements following above heading -->
    <xsl:apply-templates select="//text" />
    </fieldset>
    </xsl:template>
    <xsl:template match="text">
    <p>
    <xsl:value-of select="."/>
    </p>
    </xsl:template>
    </xsl:stylesheet>

    Where my comment is in the XSLT above I need help. I've looked at
    following-sibling which could be what I need but I can't figure out how...
    anyone?



    On 18-11-2008 22:40:30, "Hvid Hat" wrote:
    Hi
    >
    Can anyone help me with the following transition? My problem is how to
    create a fieldset each time I run into a heading and then include the
    following text elements within the fieldset.
    >
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <field>
    <heading>Firs t heading</heading>
    </field>
    <field>
    <text>Lorem</text>
    </field>
    <field>
    <text>Ipsum</text>
    </field>
    <field>
    <heading>Seco nd heading</heading>
    </field>
    <field>
    <text>Dolor</text>
    </field>
    <field>
    <text>Sit</text>
    </field>
    <field>
    <heading>Thir d heading</heading>
    </field>
    <field>
    <text>Amet</text>
    </field>
    >
    To
    >
    <fieldset>
    <legend>First heading</legend>
    <p>Lorem</p>
    <p>Ipsum</p>
    </fieldset>
    <fieldset>
    <legend>Secon d heading</legend>
    <p>Dolor</p>
    <p>Sit</p>
    </fieldset>
    <fieldset>
    <legend>Third heading</legend>
    <p>Amet</p>
    </fieldset>

    Comment

    • Mukul Gandhi

      #3
      Re: Help needed with transition

      David has given both a 2.0 and a 1.0 solution. I wouldn't write
      anything different for what David wrote for XSLT 2.0.

      But here's a slightly different 1.0 solution,

      <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
      version="1.0">

      <xsl:output method="xml" indent="yes" />

      <xsl:template match="x">
      <result>
      <xsl:apply-templates select="field[heading]" mode="a" />
      </result>
      </xsl:template>

      <xsl:template match="field" mode="a">
      <fieldset>
      <legend><xsl:va lue-of select="heading " /></legend>
      <xsl:apply-templates select="followi ng-sibling::field[1][text]"
      mode="b" />
      </fieldset>
      </xsl:template>

      <xsl:template match="field" mode="b">
      <p><xsl:value-of select="text" /></p>
      <xsl:apply-templates select="followi ng-sibling::field[1][text]"
      mode="b" />
      </xsl:template>

      </xsl:stylesheet>

      this is popularly known as the sibling recursion technique.

      Cheers,
      Mukul

      On Nov 19, 2:40 am, "Hvid Hat" <hvid....@pleas e-no-mail.thxwrote:
      Hi
      >
      Can anyone help me with the following transition? My problem is how to create
      a fieldset each time I run into a heading and then include the following text
      elements within the fieldset.
      >
      <?xml version="1.0" encoding="ISO-8859-1"?>
      <field>
        <heading>Firs t heading</heading>
      </field>
      <field>
        <text>Lorem</text>
      </field>
      <field>
        <text>Ipsum</text>
      </field>
      <field>
        <heading>Seco nd heading</heading>
      </field>
      <field>
        <text>Dolor</text>
      </field>
      <field>
        <text>Sit</text>
      </field>
      <field>
        <heading>Thir d heading</heading>
      </field>
      <field>
        <text>Amet</text>
      </field>
      >
      To
      >
      <fieldset>
        <legend>First heading</legend>
        <p>Lorem</p>
        <p>Ipsum</p>
      </fieldset>
      <fieldset>
        <legend>Secon d heading</legend>
        <p>Dolor</p>
        <p>Sit</p>
      </fieldset>
      <fieldset>
        <legend>Third heading</legend>
        <p>Amet</p>
      </fieldset>

      Comment

      Working...