supress matching

Discussion in 'XML' started by patrik.nyman@orient.su.se, Jan 9, 2007.

  1. Guest

    Suppose I have the following xml-text:

    Some text before.<A/>
    <B>Text in the B-element.</B>
    Some text after.

    And I want:

    Some text before.
    <C>Text in the B-element.</C>
    Some text after

    I have tried something like:

    <xsl:template match="A">
    <C><xsl:apply-templates select="B"/></C>
    </xsl:template>

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

    But that results in:

    Some text before.
    <C>Text in the B-element.</C>
    Text in the B-element.
    Some text after

    So basically, how can I call template B from
    within template A, and then not having B match
    again?
    , Jan 9, 2007
    #1
    1. Advertising

  2. wrote:

    > Suppose I have the following xml-text:
    >
    > Some text before.<A/>
    > <B>Text in the B-element.</B>
    > Some text after.


    That stuff is not well-formed. At least show us the element that markup
    is contained in.
    > And I want:
    >
    > Some text before.
    > <C>Text in the B-element.</C>
    > Some text after


    So you want to remove the empty A element, you want to replace the B
    element with a C element but preserves its content. And you want to
    copy text nodes outside of B. So doing e.g.

    <xsl:template match="A"/>

    ignores the A elements,

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

    transforms B into C elements and the built-in template

    <xsl:template match="text()">
    <xsl:value-of select="."/>
    </xsl:template>

    copies text nodes.



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 9, 2007
    #2
    1. Advertising

  3. wrote:
    > Suppose I have the following xml-text:
    >
    > Some text before.<A/>
    > <B>Text in the B-element.</B>
    > Some text after.
    >
    > And I want:
    >
    > Some text before.
    > <C>Text in the B-element.</C>
    > Some text after
    >
    > I have tried something like:
    >
    > <xsl:template match="A">
    > <C><xsl:apply-templates select="B"/></C>
    > </xsl:template>
    >
    > <xsl:template match="B">
    > <xsl:apply-templates/>
    > </xsl:template>
    >
    > But that results in:
    >
    > Some text before.
    > <C>Text in the B-element.</C>
    > Text in the B-element.
    > Some text after
    >
    > So basically, how can I call template B from
    > within template A, and then not having B match
    > again?
    >


    <xsl:template match="A"/>

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


    // Magnus
    Magnus Henriksson, Jan 9, 2007
    #3
  4. Guest

    OK, sorry, I tried to make it short, but it became
    confusing instead. Here's the whole story.

    I'm working with a text that contains the following
    structures:

    <p>
    Some lines of text.
    Some lines of text. *)
    </p>

    <footnote>
    Text in the footnote. It continues ...
    </footnote>

    <pagebreak/>

    <p>
    Some more lines of text.
    </p>

    <footnote>
    ... in the next footnote element!
    </footnote>

    I need to preserve this structure, but I also need to
    be able to output

    Some lines of text.
    Some lines of text. *)\footnote{%
    Text in the footnote. It continues ...
    ... in the next footnote element!
    }

    Some more lines of text.

    To this end, I apply the following markup:

    <p>
    Some lines of text.
    Some lines of text. *)<fn n="1"/>
    </p>

    <footnote n="1">
    Text in the footnote. It continues ...
    </footnote>

    <pagebreak/>

    <p>
    Some more lines of text.
    </p>

    <footnote n="1">
    ... in the next footnote element!
    </footnote>

    I have tried the following templates:

    <xsl:template match="fn">
    <xsl:param name="notenr" select="@n"/>\footnote{%
    <xsl:apply-templates select="following::fotnottext[@n eq
    $notenr]"/>}
    </xsl:template>

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

    But I get:

    Some lines of text.
    Some lines of text. *)\footnote{%
    Text in the footnote. It continues ...
    ... in the next footnote element!
    }

    Text in the footnote. It continues ... <-----

    Some more lines of text.

    ... in the next footnote element! <-----

    The lines marked with <----- must be removed somehow.
    I hope this makes it clearer what I want.
    , Jan 9, 2007
    #4
  5. Read about the "mode" attribute of the <xsl:template> instruction and of the
    <xsl:apply-templates> instruction.

    Then use this attribute in your XSLT code.


    Cheers,
    Dimitre Novatchev



    <> wrote in message
    news:...
    > OK, sorry, I tried to make it short, but it became
    > confusing instead. Here's the whole story.
    >
    > I'm working with a text that contains the following
    > structures:
    >
    > <p>
    > Some lines of text.
    > Some lines of text. *)
    > </p>
    >
    > <footnote>
    > Text in the footnote. It continues ...
    > </footnote>
    >
    > <pagebreak/>
    >
    > <p>
    > Some more lines of text.
    > </p>
    >
    > <footnote>
    > ... in the next footnote element!
    > </footnote>
    >
    > I need to preserve this structure, but I also need to
    > be able to output
    >
    > Some lines of text.
    > Some lines of text. *)\footnote{%
    > Text in the footnote. It continues ...
    > ... in the next footnote element!
    > }
    >
    > Some more lines of text.
    >
    > To this end, I apply the following markup:
    >
    > <p>
    > Some lines of text.
    > Some lines of text. *)<fn n="1"/>
    > </p>
    >
    > <footnote n="1">
    > Text in the footnote. It continues ...
    > </footnote>
    >
    > <pagebreak/>
    >
    > <p>
    > Some more lines of text.
    > </p>
    >
    > <footnote n="1">
    > ... in the next footnote element!
    > </footnote>
    >
    > I have tried the following templates:
    >
    > <xsl:template match="fn">
    > <xsl:param name="notenr" select="@n"/>\footnote{%
    > <xsl:apply-templates select="following::fotnottext[@n eq
    > $notenr]"/>}
    > </xsl:template>
    >
    > <xsl:template match="footnote">
    > <xsl:apply-templates/>
    > </xsl:template>
    >
    > But I get:
    >
    > Some lines of text.
    > Some lines of text. *)\footnote{%
    > Text in the footnote. It continues ...
    > ... in the next footnote element!
    > }
    >
    > Text in the footnote. It continues ... <-----
    >
    > Some more lines of text.
    >
    > ... in the next footnote element! <-----
    >
    > The lines marked with <----- must be removed somehow.
    > I hope this makes it clearer what I want.
    >
    Dimitre Novatchev, Jan 9, 2007
    #5
  6. In article <>,
    <> wrote:

    > Some text before.<A/>
    > <B>Text in the B-element.</B>
    > Some text after.
    >
    >And I want:
    >
    > Some text before.
    > <C>Text in the B-element.</C>
    > Some text after
    >
    >I have tried something like:
    >
    > <xsl:template match="A">
    > <C><xsl:apply-templates select="B"/></C>
    > </xsl:template>
    >
    > <xsl:template match="B">
    > <xsl:apply-templates/>
    > </xsl:template>


    You can't be doing that! The B is not inside the A so select="B"
    won't select anything. Presumably you are using following-sibling or
    something similar.

    You could change the template of the element containing A and B to
    not call apply-templates on B - perhaps:

    <xsl:apply-templates select="*[not(self::B)]"/>

    or you could use modes. Have a plain template for B that does nothing,
    and a template in mode really-do-it:

    <xsl:template match="B" mode="really-do-it">
    <xsl:apply-templates/>
    </xsl:template>

    Then have the template for A call apply-templates on B with
    mode="really-do-it".

    -- Richard
    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Jan 9, 2007
    #6
  7. Guest

    Richard Tobin wrote:

    > You can't be doing that! The B is not inside the A so select="B"
    > won't select anything. Presumably you are using following-sibling or
    > something similar.


    You're right, of course. I over-simplified my example.

    > or you could use modes. Have a plain template for B that does nothing,
    > and a template in mode really-do-it:


    Yes, that does it. Wonderful! Thanks a lot to all who answered.

    /Patrik
    , Jan 9, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JG
    Replies:
    3
    Views:
    4,003
    Scott Allen
    Oct 21, 2004
  2. Edwin Knoppert

    Supress error/warning of winapi in class?

    Edwin Knoppert, Dec 13, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    950
    Edwin Knoppert
    Dec 13, 2005
  3. Supress output

    , Feb 14, 2006, in forum: XML
    Replies:
    1
    Views:
    392
    Richard Tobin
    Feb 14, 2006
  4. roderik

    psycopg supress output

    roderik, Nov 15, 2004, in forum: Python
    Replies:
    1
    Views:
    266
    Michael Fuhr
    Nov 16, 2004
  5. Thomas Guettler

    supress creation of .pyc files

    Thomas Guettler, Feb 16, 2005, in forum: Python
    Replies:
    3
    Views:
    319
    =?ISO-8859-15?Q?=22Martin_v=2E_L=F6wis=22?=
    Feb 16, 2005
Loading...

Share This Page