XSLT:best manner to browse a file

Discussion in 'XML' started by naima.mans@gmail.com, Dec 20, 2006.

  1. Guest

    Hello :)

    I'm newbie to XSLT and i need some advice please:

    Here the action:
    I have to browse an XML file with xslt :
    For each node i have to determinate if it is a node where i need to add
    an attribute...

    The question is:
    What is the best manner to browse the xml file? (prefix browse,
    postfixe browse??)

    an example:
    THE XML FILE:
    <persone>
    <id>123</id>
    <name>samantha</name>
    <adresse>
    <id>abc</id>
    <road> wall street </road>
    </adresse>
    </personne>

    THE RESULT after XSLT transformation: (i have copy the ID attribut in
    the father node)

    <persone id=123>
    <id>123</id>
    <name>samantha</name>
    <adresse id=abc>
    <id>abc</id>
    <road> wall street </road>
    </adresse>
    </personne>


    thanks a lot

    Tachi
     
    , Dec 20, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Here the action:
    > I have to browse an XML file with xslt :
    > For each node i have to determinate if it is a node where
    > i need to add an attribute...


    It's easily done with identity transformation and exclusion
    templates. By the way, the wording of your question
    probably indicates that you're thinking about your problem
    in terms of imperative programming. That's generally a very
    bad idea when working with XSLT. You should be thinking in
    terms of template matches and selecting nodesets you need,
    that'll make it so much easier to figure out how you should
    go about your problems.

    > What is the best manner to browse the xml file? (prefix
    > browse, postfixe browse??)


    I'm not sure what you mean by prefix browse/postfix browse,
    or, for that matter, what do you mean by browse where XSLT
    is concerned, but as I said, using the identity
    transformation seems to be the best way to solve your
    problem.

    > THE XML FILE:
    > <persone>
    > <id>123</id>
    > <name>samantha</name>
    > <adresse>
    > <id>abc</id>
    > <road> wall street </road>
    > </adresse>
    > </personne>


    That's not well-formed. It's a good idea to post examples
    without obvious, easily fixable mistakes when you're asking
    for help. Posting examples *with* obvious, easily fixable
    mistakes is a very *bad* idea on the other hand.

    > <persone id=123>
    > <id>123</id>
    > <name>samantha</name>
    > <adresse id=abc>
    > <id>abc</id>
    > <road> wall street </road>
    > </adresse>
    > </personne>


    That's, uh, even less well-formed. XML 1.0 spec clearly
    states (see the AttValue definition) that attribute values
    must be enclosed in either quotes or apostrophes.

    The following transformation should work:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- identity transformation -->
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>
    <!-- exclusion template -->
    <xsl:template match="*[id]">
    <xsl:copy>
    <xsl:attribute name="id">
    <xsl:value-of select="id"/>
    </xsl:attribute>
    <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

    --
    Pavel Lepin
     
    , Dec 20, 2006
    #2
    1. Advertising

  3. Guest

    Hello M lepin

    ho sorry for the example..

    thanks a lot for your help..

    i have use the identity method and adapt your XSLT.. it works now

    i have a question about you code:
    <xsl:template match="*[id]">
    what does *[id] means please?

    For information here is the worked code... thanks again:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes"
    />

    <!-- identity transformation -->
    <xsl:template match="@*|node()">
    <xsl:copy>
    <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
    </xsl:template>

    <!-- exclusion template -->
    <xsl:template match="//THE_NODE_1_WICH_WILL_BE_ENRICH">
    <xsl:copy>
    <xsl:for-each select="THE_NODE_WICH_WILL_ENRICH_THE_NODE_1">
    <xsl:attribute name="{local-name(.)}">
    <xsl:value-of select="VALUE" />
    </xsl:attribute>
    </xsl:for-each>
    <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>
     
    , Dec 20, 2006
    #3
  4. Guest

    Please quote what you're replying to. Not everyone here
    is using Google Groups to read the newsgroups. The proper
    etiquette is to provide some context by quoting parts of
    the original post you're replying to (and only those parts)
    and insert your replies under the relevant quotes.

    wrote:
    > i have a question about you code:
    > <xsl:template match="*[id]">
    > what does *[id] means please?


    You should try reading some decent XPath tutorial. In this
    case *[id] means that this template matches any element
    nodes that have element children named 'id'.

    > <!-- exclusion template -->
    > <xsl:template match="//THE_NODE_1_WICH_WILL_BE_ENRICH">
    > <xsl:copy>
    > <xsl:for-each
    > select="THE_NODE_WICH_WILL_ENRICH_THE_NODE_1">
    > <xsl:attribute name="{local-name(.)}">
    > <xsl:value-of select="VALUE" />
    > </xsl:attribute>
    > </xsl:for-each>


    That doesn't look too good, but, heck, whatever works for
    you.

    > <xsl:apply-templates select="@*|node()" />
    > </xsl:copy>
    > </xsl:template>


    --
    Pavel Lepin
     
    , Dec 20, 2006
    #4
  5. Guest

    a écrit :

    > > <!-- exclusion template -->
    > > <xsl:template match="//THE_NODE_1_WICH_WILL_BE_ENRICH">
    > > <xsl:copy>
    > > <xsl:for-each
    > > select="THE_NODE_WICH_WILL_ENRICH_THE_NODE_1">
    > > <xsl:attribute name="{local-name(.)}">
    > > <xsl:value-of select="VALUE" />
    > > </xsl:attribute>
    > > </xsl:for-each>

    >
    > That doesn't look too good, but, heck, whatever works for
    > you.
    >
    > > <xsl:apply-templates select="@*|node()" />
    > > </xsl:copy>
    > > </xsl:template>

    >


    ha... any suggestion will be very appreciate... why it doesn't look
    good?

    thanks

    tachi
     
    , Dec 20, 2006
    #5
  6. Guest

    wrote:
    > a écrit :
    > > > <!-- exclusion template -->
    > > > <xsl:template
    > > > match="//THE_NODE_1_WICH_WILL_BE_ENRICH">
    > > > <xsl:copy>
    > > > <xsl:for-each
    > > > select="THE_NODE_WICH_WILL_ENRICH_THE_NODE_1">
    > > > <xsl:attribute name="{local-name(.)}">
    > > > <xsl:value-of select="VALUE" />
    > > > </xsl:attribute>
    > > > </xsl:for-each>

    > >
    > > That doesn't look too good, but, heck, whatever works
    > > for you.
    > >
    > > > <xsl:apply-templates select="@*|node()" />
    > > > </xsl:copy>
    > > > </xsl:template>

    >
    > ha... any suggestion will be very appreciate... why it
    > doesn't look good?


    It's hard to tell whether there's anything wrong with the
    code without a real example (the above is pseudo-codish if
    I got that right), but, generally, for-each in neophytes'
    code is a bad sign. It seems to denote that they're
    thinking about their transformations in inappropriate
    terms.

    XSLT is a functional language at heart, and it simply
    doesn't work all that well if you're trying to use it
    imperatively. It's not even the question of conceptual
    purity,--whatever works, as I said,--it's just that
    approaching problems the same way you did while coding in
    C++/Java/Perl/PHP/{imperative language of your choice}
    tends to lead you down a lot of blind alleys. Getting the
    right mindset from the start seems to help a lot.

    --
    Pavel Lepin
     
    , Dec 21, 2006
    #6
  7. Guest

    Hello M lepin ,

    I see...

    i'm going to change my "programming" reflexion into an "xml" one ..

    thanks a lot for your advices...

    ++

    tachi

    a écrit :

    > wrote:
    > > a écrit :
    > > > > <!-- exclusion template -->
    > > > > <xsl:template
    > > > > match="//THE_NODE_1_WICH_WILL_BE_ENRICH">
    > > > > <xsl:copy>
    > > > > <xsl:for-each
    > > > > select="THE_NODE_WICH_WILL_ENRICH_THE_NODE_1">
    > > > > <xsl:attribute name="{local-name(.)}">
    > > > > <xsl:value-of select="VALUE" />
    > > > > </xsl:attribute>
    > > > > </xsl:for-each>
    > > >
    > > > That doesn't look too good, but, heck, whatever works
    > > > for you.
    > > >
    > > > > <xsl:apply-templates select="@*|node()" />
    > > > > </xsl:copy>
    > > > > </xsl:template>

    > >
    > > ha... any suggestion will be very appreciate... why it
    > > doesn't look good?

    >
    > It's hard to tell whether there's anything wrong with the
    > code without a real example (the above is pseudo-codish if
    > I got that right), but, generally, for-each in neophytes'
    > code is a bad sign. It seems to denote that they're
    > thinking about their transformations in inappropriate
    > terms.
    >
    > XSLT is a functional language at heart, and it simply
    > doesn't work all that well if you're trying to use it
    > imperatively. It's not even the question of conceptual
    > purity,--whatever works, as I said,--it's just that
    > approaching problems the same way you did while coding in
    > C++/Java/Perl/PHP/{imperative language of your choice}
    > tends to lead you down a lot of blind alleys. Getting the
    > right mindset from the start seems to help a lot.
    >
    > --
    > Pavel Lepin
     
    , Dec 21, 2006
    #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. Replies:
    3
    Views:
    892
    James Kanze
    Jun 20, 2007
  2. decker
    Replies:
    9
    Views:
    336
    Barry Schwarz
    Nov 10, 2007
  3. Replies:
    46
    Views:
    2,698
    Tim Rentsch
    Aug 7, 2010
  4. Mats Olsson
    Replies:
    9
    Views:
    382
    Just1Coder
    Sep 14, 2004
  5. Replies:
    0
    Views:
    213
Loading...

Share This Page