Preserving text position but transforming sub elements.

Discussion in 'XML' started by Matthew Rees-George, Jul 31, 2003.

  1. I want this example xml:-
    <xxx>text before <yyy att="value"/> text in the middle <zzz>ignored
    but transformed</zzz> text after</xxx>

    To be transformed to this:-
    text before value text in the middle 'found a zzz' text after

    I.e. the text positions must be preserved, but the element(s) must be
    transformed.

    I could not find similar problems or solutions on the web or
    newsgroups, but I have this solution, which I really, really do not
    like:-

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

    <xsl:eek:utput method="text" indent="yes" encoding="utf-8"/>

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

    <xsl:template match="xxx">
    <xsl:for-each select="node()">
    <xsl:choose>
    <xsl:when test="name(.)='yyy'"><xsl:value-of
    select="@att"/></xsl:when>
    <xsl:when test="name(.)='zzz'">'found a zzz'</xsl:when>
    <xsl:eek:therwise><xsl:copy/></xsl:eek:therwise>
    </xsl:choose>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

    There must be a more obvious solution? Anyone?
    Matthew Rees-George, Jul 31, 2003
    #1
    1. Advertising

  2. (Matthew Rees-George) wrote in
    news::

    > I could not find similar problems or solutions on the web or
    > newsgroups, but I have this solution, which I really, really do
    > not like:-
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:eek:utput method="text" indent="yes" encoding="utf-8"/>
    >
    > <xsl:template match="/">
    > <xsl:apply-templates select="xxx"/>
    > </xsl:template>
    >
    > <xsl:template match="xxx">
    > <xsl:for-each select="node()">
    > <xsl:choose>
    > <xsl:when test="name(.)='yyy'"><xsl:value-of
    > select="@att"/></xsl:when>
    > <xsl:when test="name(.)='zzz'">'found a
    > zzz'</xsl:when>
    > <xsl:eek:therwise><xsl:copy/></xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:for-each>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > There must be a more obvious solution? Anyone?


    Repeat after me: "Simpler is better".

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

    <xsl:eek:utput method="text" indent="no" encoding="utf-8"/>

    <!--First, match the xxx element and process its content -->

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

    <!--Now let's deal with yyy -->

    <xsl:template match="yyy">
    <xsl:value-of select="@att"/>
    </xsl:template>

    <!--Finally, take care of the zzz element -->

    <xsl:template match="zzz">
    <xsl:text> 'found a zzz' </xsl:text>
    </xsl:template>

    </xsl:stylesheet>

    --
    a. m. slotnik
    arnold m. slotnik, Jul 31, 2003
    #2
    1. Advertising

  3. Use:

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

    <xsl:eek:utput method="text"/>

    <xsl:template match="@*">
    <xsl:value-of select="."/>
    </xsl:template>

    <xsl:template match="zzz">'found a zzz'</xsl:template>

    </xsl:stylesheet>



    =====
    Cheers,

    Dimitre Novatchev.
    http://fxsl.sourceforge.net/ -- the home of FXSL


    "Matthew Rees-George" <> wrote in message
    news:...
    > I want this example xml:-
    > <xxx>text before <yyy att="value"/> text in the middle <zzz>ignored
    > but transformed</zzz> text after</xxx>
    >
    > To be transformed to this:-
    > text before value text in the middle 'found a zzz' text after
    >
    > I.e. the text positions must be preserved, but the element(s) must be
    > transformed.
    >
    > I could not find similar problems or solutions on the web or
    > newsgroups, but I have this solution, which I really, really do not
    > like:-
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:eek:utput method="text" indent="yes" encoding="utf-8"/>
    >
    > <xsl:template match="/">
    > <xsl:apply-templates select="xxx"/>
    > </xsl:template>
    >
    > <xsl:template match="xxx">
    > <xsl:for-each select="node()">
    > <xsl:choose>
    > <xsl:when test="name(.)='yyy'"><xsl:value-of
    > select="@att"/></xsl:when>
    > <xsl:when test="name(.)='zzz'">'found a zzz'</xsl:when>
    > <xsl:eek:therwise><xsl:copy/></xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:for-each>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > There must be a more obvious solution? Anyone?
    Dimitre Novatchev, Jul 31, 2003
    #3
  4. <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="text" indent="no" encoding="utf-8"/>

    <!--First, match the xxx element and process its content -->
    <xsl:template match="xxx">
    <xsl:apply-templates/>
    </xsl:template>

    <!--Now let's deal with yyy -->
    <xsl:template match="yyy">
    <xsl:value-of select="@att"/>
    </xsl:template>

    <!--Finally, take care of the zzz element -->
    <xsl:template match="zzz">
    <xsl:text> 'found a zzz' </xsl:text>
    </xsl:template>

    </xsl:stylesheet>

    When you say it like that, it almost makes sense. But do you really think
    that people are going to enter their expletives in zzz-tags? ;-)

    I think after two months that XSLT is starting to sink into my brain. I've
    had trouble convincing myself that templates were really function
    definitions and that apply-templates was really a "run-source" command. I
    guess you don't expect the program flow to be controlled by your input
    parameter.

    Strange world, strange syntax. Thanks both for the revelation.

    One question: is it important to deal with xxx first? I suspect not, and
    I'm used to defining functions before I attempt to call them, so I would
    probably graduate to doing that last.
    Christopher Boomer, Jul 31, 2003
    #4
  5. "Christopher Boomer" < (don't dash!)>
    wrote in news::

    > One question: is it important to deal with xxx first? I suspect
    > not, and I'm used to defining functions before I attempt to call
    > them, so I would probably graduate to doing that last.


    No, the order of the templates in the stylesheet in this case don't
    matter.

    --
    a. m. slotnik
    arnold m. slotnik, Jul 31, 2003
    #5
  6. Thanks, I knew it was blatently obvious ~ I just couldn't see the wood
    for the trees (common problem in XSLT?). I forgot that you could
    apply-templates without a select ~ and I have always selected. Learnt
    my lesson.
    Matthew Rees-George, Jul 31, 2003
    #6
  7. "Christopher Boomer" < (don't dash!)> wrote in
    message news:...
    >
    > I've had trouble convincing myself that templates were really function
    > definitions and that apply-templates was really a "run-source" command.
    > I guess you don't expect the program flow to be controlled by your input
    > parameter.
    >


    It is best to throw out all concepts of "program flow", "functions", etc.
    This is more like CSS: a set of patterns and corresponding outcomes.
    (Or like awk or prolog).
    The "input parameter" is what matches the pattern and activates the rule.

    It is like saying "Put all nails in the blue drawer and all screws in the
    green drawer". There is no assumption that the mixture of nails/screws
    will be in any order, or that the rules will be handled sequentially.

    Of course, selectively applying particular templates to a particular
    sub-tree can make it look like a program, and the 'if' and 'for-each'
    tags are very program-like. However, you can do most simple xsl
    without either selecting templates or using batch-processing nodes.

    --
    Steve
    Steven Dilley, Aug 1, 2003
    #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. TTroy
    Replies:
    16
    Views:
    777
    Peter Nilsson
    Jan 31, 2005
  2. Ben
    Replies:
    2
    Views:
    856
  3. Replies:
    4
    Views:
    500
  4. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,954
    Lawrence D'Oliveiro
    May 20, 2011
  5. Felix
    Replies:
    3
    Views:
    129
    Felix
    Feb 23, 2004
Loading...

Share This Page