XSL Umformung

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sascha Kerschhofer

    XSL Umformung

    Hi,
    ich suche eine Idee, wie man folgendes mit XSL erreichen kann: folgendes XML
    Dokument:
    <body>
    Erster Text<br/>
    Zweiter Text<br/>
    <em>Dritter</em> Text<br/>
    Vierter <u>Text</u><br clear-previous="true"/>

    Fünfter Text<br/>

    Sechster Text
    </body>

    Ich will das nun in einzelne Absätze teilen, aber so, dass der Text im
    Absatz es immer "kumulativ" wird außer clear-previous ist true (die übrigen
    Formatierungen müssen aber erhalten bleiben). Das würde liefern:

    <body>
    <p>Erster Text</p>
    <p>Erster TextZweiterText </p>
    <p>Erster TextZweiterText <em>Dritter</em> Text</p>
    <p>Vierter <u>Text</u></p>
    <p>Vierter <u>Text</u>Fünfter Text</p>
    <p>Vierter <u>Text</u>Fünfter TextSechster Text</p>
    </body>

    Hat jemand eine Idee oder Lösung?

    Sascha Kerschhofer


  • Sascha Kerschhofer

    #2
    Re: XSL Umformung

    Sorry, wrong Newsgroup selected...


    "Sascha Kerschhofer" <skerschhofer@y ahoo.antispam.d e> schrieb im Newsbeitrag
    news:d9h49h$d25 $1@news.mch.sbs .de...[color=blue]
    > Hi,
    > ich suche eine Idee, wie man folgendes mit XSL erreichen kann: folgendes[/color]
    XML[color=blue]
    > Dokument:
    > <body>
    > Erster Text<br/>
    > Zweiter Text<br/>
    > <em>Dritter</em> Text<br/>
    > Vierter <u>Text</u><br clear-previous="true"/>
    >
    > Fünfter Text<br/>
    >
    > Sechster Text
    > </body>
    >
    > Ich will das nun in einzelne Absätze teilen, aber so, dass der Text im
    > Absatz es immer "kumulativ" wird außer clear-previous ist true (die[/color]
    übrigen[color=blue]
    > Formatierungen müssen aber erhalten bleiben). Das würde liefern:
    >
    > <body>
    > <p>Erster Text</p>
    > <p>Erster TextZweiterText </p>
    > <p>Erster TextZweiterText <em>Dritter</em> Text</p>
    > <p>Vierter <u>Text</u></p>
    > <p>Vierter <u>Text</u>Fünfter Text</p>
    > <p>Vierter <u>Text</u>Fünfter TextSechster Text</p>
    > </body>
    >
    > Hat jemand eine Idee oder Lösung?
    >
    > Sascha Kerschhofer
    >
    >[/color]


    Comment

    • Joris Gillis

      #3
      Re: XSL Umformung

      On Fri, 24 Jun 2005 16:10:57 +0200, Sascha Kerschhofer
      <skerschhofer@y ahoo.antispam.d e> wrote:
      [color=blue]
      > Ich will das nun in einzelne Absätze teilen, aber so, dass der Text im
      > Absatz es immer "kumulativ" wird außer clear-previous ist true (die
      > übrigen
      > Formatierungen müssen aber erhalten bleiben). Das würde liefern:[/color]

      Hi,

      Here you find a working solution.
      It uses a combination of a key method and a recursivly called template.

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

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

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

      <xsl:template match="body">
      <xsl:copy>
      <xsl:apply-templates select="br"/>
      <xsl:if test="br[last()]/following-sibling::node() ">
      <p>
      <xsl:apply-templates select="br[last()]" mode="copy_tail "/>
      <xsl:copy-of select="br[last()]/following-sibling::node() "/>
      </p>
      </xsl:if>
      </xsl:copy>
      </xsl:template>

      <xsl:template match="br">
      <p>
      <xsl:if test="not(@clea r-previous='true' )">
      <xsl:apply-templates select="precedi ng-sibling::br[1]"
      mode="copy_tail "/>
      </xsl:if>
      <xsl:copy-of select="key('be fore_br',genera te-id())"/>
      </p>
      </xsl:template>

      <xsl:template match="br" mode="copy_tail ">
      <xsl:apply-templates select="precedi ng-sibling::br[1]" mode="copy_tail "/>
      <xsl:copy-of select="key('be fore_br',genera te-id())"/>
      </xsl:template>

      <xsl:template match="br[@clear-previous='true']" mode="copy_tail ">
      <xsl:copy-of select="key('be fore_br',genera te-id())"/>
      </xsl:template>

      </xsl:stylesheet>

      regards,
      --
      Joris Gillis (http://users.telenet.be/root-jg/me.html)
      Vincit omnia simplicitas
      Keep it simple

      Comment

      • Sascha Kerschhofer

        #4
        Re: XSL Umformung

        Thank you Joris,
        this works pretty well. Honestly this is a quite interesting but however a
        guru-stylesheet and I don't understand exactly how it works. What is the
        recursion-rule? Maybe there is a chance to get it explained :).
        Unfortunately I had made a mistake in my example since the "new" text group
        must be started after a br[@clear-previous="true"]. The example below is
        corrected now. So what need to be modified?

        For all not-german speaking people here is the actual question:

        I need to transform some text that is divided by empty br-elements into
        separate paragraphs. Further all separated text fragments must be
        progressively collected. Example:

        <body>
        First Text<br/>
        Second Text<br/>
        <em>Thrird</em> Text<br/>
        Fourth <u>Text</u><br clear-previous="true"/>
        Fifth Text<br/>
        Sixt Text
        </body>

        becomes

        <body>
        <p>First Text</p>
        <p>First TextZweiter Text</p>
        <p>First TextSecond Text<em>Third</em> Text</p>
        <p>First TextSecond Text<em>Third</em> TextFourth <u>Text</u></p>
        <p>Fitht Text</p>
        <p>Fith TextSixt Text</p>
        </body>


        Sascha Kerschhofer


        Comment

        • Joris Gillis

          #5
          Re: XSL Umformung

          Hi,

          On Tue, 28 Jun 2005 14:28:54 +0200, Kerschhofer Alexander
          <alexander.kers chhofer@siemens .com> wrote:

          Honestly this is a quite interesting but however a guru-stylesheet
          It would take a guru to code a guru-stylesheet:D Mine was probably
          unintelligable. ..
          and I don't understand exactly how it works. What is the recursion-rule?
          The recursion is done via the 'copy_tail' template
          Maybe there is a chance to get it explained :).
          see the comments below
          Unfortunately I had made a mistake in my example since the "new" text group
          must be started after a br[@clear-previous="true"]. The example below is
          corrected now. So what need to be modified?

          In that case it becomes much easier, it was actually the error in your
          sample data that made the job challenging.
          Here you have a new stylesheet with comments included:

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

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

          <xsl:key name="before_br " match="node()[not(self::br)]"
          use="generate-id(following-sibling::br[1])"/>
          <!-- the key connects all nodes to their first 'br' element
          following-sibling -->

          <xsl:template match="body">
          <xsl:copy>
          <xsl:apply-templates select="br"/>
          <xsl:if test="br[last()]/following-sibling::node() ">
          <!-- handle left-over nodes at the body's end-->
          <p>
          <xsl:apply-templates select="br[last()]" mode="copy_tail "/>
          <xsl:copy-of select="br[last()]/following-sibling::node() "/>
          </p>
          </xsl:if>
          </xsl:copy>
          </xsl:template>

          <xsl:template match="br">
          <p>
          <xsl:apply-templates select="." mode="copy_tail "/>
          <!-- copy the tail of this 'br' node inside a 'p' element-->
          </p>
          </xsl:template>

          <xsl:template match="br" mode="copy_tail ">
          <!-- continue copying the tail backwards and only stop when the
          'clear-previous' attribute is 'true'-->
          <xsl:apply-templates
          select="precedi ng-sibling::br[1][not(@clear-previous='true' )]"
          mode="copy_tail "/>
          <xsl:copy-of select="key('be fore_br',genera te-id())"/>
          </xsl:template>

          </xsl:stylesheet>


          regards,
          --
          Joris Gillis (http://users.telenet.be/root-jg/me.html)
          Vincit omnia simplicitas
          Keep it simple

          Comment

          • Sascha Kerschhofer

            #6
            Re: XSL Umformung

            Wow, so you are indeed a guru xsl-transformer...
            Sascha
            [color=blue]
            > In that case it becomes much easier, it was actually the error in your
            > sample data that made the job challenging.
            > Here you have a new stylesheet with comments included:
            >
            > <xsl:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
            > version="1.0">
            >
            > <xsl:output method="xml" indent="yes"/>
            >
            > <xsl:key name="before_br " match="node()[not(self::br)]"
            > use="generate-id(following-sibling::br[1])"/>
            > <!-- the key connects all nodes to their first 'br' element
            > following-sibling -->
            >
            > <xsl:template match="body">
            > <xsl:copy>
            > <xsl:apply-templates select="br"/>
            > <xsl:if test="br[last()]/following-sibling::node() ">
            > <!-- handle left-over nodes at the body's end-->
            > <p>
            > <xsl:apply-templates select="br[last()]" mode="copy_tail "/>
            > <xsl:copy-of select="br[last()]/following-sibling::node() "/>
            > </p>
            > </xsl:if>
            > </xsl:copy>
            > </xsl:template>
            >
            > <xsl:template match="br">
            > <p>
            > <xsl:apply-templates select="." mode="copy_tail "/>
            > <!-- copy the tail of this 'br' node inside a 'p' element-->
            > </p>
            > </xsl:template>
            >
            > <xsl:template match="br" mode="copy_tail ">
            > <!-- continue copying the tail backwards and only stop when the
            > 'clear-previous' attribute is 'true'-->
            > <xsl:apply-templates
            > select="precedi ng-sibling::br[1][not(@clear-previous='true' )]"
            > mode="copy_tail "/>
            > <xsl:copy-of select="key('be fore_br',genera te-id())"/>
            > </xsl:template>
            >
            > </xsl:stylesheet>
            >
            >
            > regards,
            > --
            > Joris Gillis (http://users.telenet.be/root-jg/me.html)
            > Vincit omnia simplicitas
            > Keep it simple[/color]


            Comment

            • Joris Gillis

              #7
              Re: XSL Umformung

              > Wow, so you are indeed a guru xsl-transformer...
              I actually meant to say that I do not consider myself a guru at all.

              Comment

              Working...