xslt trouble with xsl:if

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

    xslt trouble with xsl:if

    hi-

    i am fooling around with soap and weather templates.
    for some reason either this if or select is failing.
    i am very new to xml and found this code at another site.
    i can show you the xml and then the xslt sample code that is not matching.
    please look and if it jumps right off the page give me a tip on why it didnt
    select the data.
    i think for most xslt people this will be easy. i want the if to work if it
    is null because
    the weather service may return null for an attribute.

    from the government weather service:
    <weather time-layout="k-p24h-n5-1">
    <name>Weather Type, Coverage, and Intensity</name>
    <weather-conditions weather-summary="Increa sing Clouds"/>
    <weather-conditions weather-summary="Partly Cloudy"/>
    <weather-conditions weather-summary="Partly Cloudy"/>
    <weather-conditions weather-summary="Mostly Sunny"/>
    <weather-conditions weather-summary="Chance Rain Showers">
    <value coverage="chanc e" intensity="ligh t" weather-type="rain
    showers" qualifier="none "/>
    </weather-conditions>
    </weather>


    from a sample template i found:
    <xsl:template name="weather">
    <xsl:param name="position" />
    <xsl:if
    test="/dwml/data/parameters/weather/weather-conditions[position()=$pos ition]/@weather-summary
    !=''">
    <xsl:value-of
    select="/dwml/data/parameters/weather/weather-conditions[position()=$pos ition]/@weather-summary"/>
    </xsl:if>
    </xsl:template>

    other parts of the templates are generating output (not formatted yet
    though)
    assuming that the path is correct and it looks like it is, why wont this
    match?
    @weather-summary is the attribute that has a value associated with it that
    is not null.
    the path from the root looks ok....

    i want to get into learning a little bit more about xml and xslt.
    i have a book on it and am currently studing a css book too.
    i like the fact that when i combine all the templates with html i can have
    it output
    the results formatted. i need to get better at the xslt processing.

    thanks very much.
    i hope you are enjoying the weekend too.
    jim (website - 3rdshiftcoder.c om)









  • Peter Flynn

    #2
    Re: xslt trouble with xsl:if

    z1 wrote:
    hi-
    >
    i am fooling around with soap and weather templates.
    for some reason either this if or select is failing.
    i am very new to xml and found this code at another site.
    i can show you the xml and then the xslt sample code that is not matching.
    please look and if it jumps right off the page give me a tip on why it didnt
    select the data.
    i think for most xslt people this will be easy. i want the if to work if it
    is null because
    the weather service may return null for an attribute.
    >
    from the government weather service:
    <weather time-layout="k-p24h-n5-1">
    <name>Weather Type, Coverage, and Intensity</name>
    <weather-conditions weather-summary="Increa sing Clouds"/>
    <weather-conditions weather-summary="Partly Cloudy"/>
    <weather-conditions weather-summary="Partly Cloudy"/>
    <weather-conditions weather-summary="Mostly Sunny"/>
    <weather-conditions weather-summary="Chance Rain Showers">
    <value coverage="chanc e" intensity="ligh t" weather-type="rain
    showers" qualifier="none "/>
    </weather-conditions>
    </weather>
    >
    >
    from a sample template i found:
    <xsl:template name="weather">
    <xsl:param name="position" />
    <xsl:if
    test="/dwml/data/parameters/weather/weather-conditions[position()=$pos ition]/@weather-summary
    !=''">
    Depending on how the incoming data is handled (parsed as well-formed
    only, or validated), position() may evaluate to one of two things:

    a) what you intuitively expect (if you're used to normal text document
    markup (HTML, DocBook, TEI, etc): the numeric position of the
    weather-conditions *element node* among its siblings (eg for
    @weather-summary='Partly Cloudy' this would be "2" and "3");

    b) what you don't expect (unless you came to XML via the "data-only"
    route): the numeric position of the matching node among *all* types of
    siblings, including the "name" element and the (otherwise irrelevant)
    white-space between the weather-conditions elements (eg for
    @weather-summary='Partly Cloudy' this would be "6" and "8").

    I'm unclear why it would want to identify the weather-conditions element
    by position anyway.

    The reliable way of matching location-among-siblings is to use
    [count(preceding-sibling::weathe r-conditions)+1=$ position]
    <xsl:value-of
    select="/dwml/data/parameters/weather/weather-conditions[position()=$pos ition]/@weather-summary"/>
    </xsl:if>
    </xsl:template>
    >
    other parts of the templates are generating output (not formatted yet
    though)
    assuming that the path is correct and it looks like it is, why wont this
    match?
    Use xsl-message and xsl:value-of to output the actual value of $position
    to the console during execution so you can debug it.
    @weather-summary is the attribute that has a value associated with it that
    is not null.
    the path from the root looks ok....
    You haven't shown us the whole document, so we'll take it on trust that
    there is a containing structure reflecting /dwml/data/parameters
    i want to get into learning a little bit more about xml and xslt.
    i have a book on it and am currently studing a css book too.
    i like the fact that when i combine all the templates with html i can have
    it output
    the results formatted. i need to get better at the xslt processing.
    Dave Pawson's excellent XSL FAQ (http://www.dpawson.co.uk/xsl/), and
    Mulberry's XSL-LIST mailing list
    (http://www.mulberrytech.com/xsl/xsl-list/index.html) are your friends.

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/

    Comment

    • z1

      #3
      Re: xslt trouble with xsl:if

      Hi Peter -

      thanks very much for the response.
      i need to practice more.
      i will save your post and can come
      back to it a little later.

      i kind of get things but need more work.
      thanks for the list of resources at the end
      of your post.

      jim

      "Peter Flynn" <peter.nosp@m.s ilmaril.iewrote in message
      news:670kanF2mn n41U1@mid.indiv idual.net...
      z1 wrote:
      >hi-
      >>
      >i am fooling around with soap and weather templates.
      >for some reason either this if or select is failing.
      >i am very new to xml and found this code at another site.
      >i can show you the xml and then the xslt sample code that is not
      >matching.
      >please look and if it jumps right off the page give me a tip on why it
      >didnt
      >select the data.
      >i think for most xslt people this will be easy. i want the if to work if
      >it
      >is null because
      >the weather service may return null for an attribute.
      >>
      >from the government weather service:
      > <weather time-layout="k-p24h-n5-1">
      > <name>Weather Type, Coverage, and Intensity</name>
      > <weather-conditions weather-summary="Increa sing Clouds"/>
      > <weather-conditions weather-summary="Partly Cloudy"/>
      > <weather-conditions weather-summary="Partly Cloudy"/>
      > <weather-conditions weather-summary="Mostly Sunny"/>
      > <weather-conditions weather-summary="Chance Rain Showers">
      > <value coverage="chanc e" intensity="ligh t" weather-type="rain
      >showers" qualifier="none "/>
      > </weather-conditions>
      > </weather>
      >>
      >>
      >from a sample template i found:
      ><xsl:templat e name="weather">
      > <xsl:param name="position" />
      > <xsl:if
      >test="/dwml/data/parameters/weather/weather-conditions[position()=$pos ition]/@weather-summary
      >!=''">
      >
      Depending on how the incoming data is handled (parsed as well-formed
      only, or validated), position() may evaluate to one of two things:
      >
      a) what you intuitively expect (if you're used to normal text document
      markup (HTML, DocBook, TEI, etc): the numeric position of the
      weather-conditions *element node* among its siblings (eg for
      @weather-summary='Partly Cloudy' this would be "2" and "3");
      >
      b) what you don't expect (unless you came to XML via the "data-only"
      route): the numeric position of the matching node among *all* types of
      siblings, including the "name" element and the (otherwise irrelevant)
      white-space between the weather-conditions elements (eg for
      @weather-summary='Partly Cloudy' this would be "6" and "8").
      >
      I'm unclear why it would want to identify the weather-conditions element
      by position anyway.
      >
      The reliable way of matching location-among-siblings is to use
      [count(preceding-sibling::weathe r-conditions)+1=$ position]
      >
      > <xsl:value-of
      >>
      >select="/dwml/data/parameters/weather/weather-conditions[position()=$pos ition]/@weather-summary"/>
      > </xsl:if>
      ></xsl:template>
      >>
      >other parts of the templates are generating output (not formatted yet
      >though)
      >assuming that the path is correct and it looks like it is, why wont this
      >match?
      >
      Use xsl-message and xsl:value-of to output the actual value of $position
      to the console during execution so you can debug it.
      >
      >@weather-summary is the attribute that has a value associated with it
      >that
      >is not null.
      >the path from the root looks ok....
      >
      You haven't shown us the whole document, so we'll take it on trust that
      there is a containing structure reflecting /dwml/data/parameters
      >
      >i want to get into learning a little bit more about xml and xslt.
      >i have a book on it and am currently studing a css book too.
      >i like the fact that when i combine all the templates with html i can
      >have
      >it output
      >the results formatted. i need to get better at the xslt processing.
      >
      Dave Pawson's excellent XSL FAQ (http://www.dpawson.co.uk/xsl/), and
      Mulberry's XSL-LIST mailing list
      (http://www.mulberrytech.com/xsl/xsl-list/index.html) are your friends.
      >
      ///Peter
      --
      XML FAQ: http://xml.silmaril.ie/

      Comment

      • z1

        #4
        Re: xslt trouble with xsl:if

        Hi again Peter-

        I am finally getting this stuff better.

        What had me stuck was that somehow the file i had copied from the government
        web server for the weather wasnt quite right. you can put in all the xpath
        and
        xslt you want but if the xml file is broken that you are basing it on you
        are
        nowhere. i must not have copied it just right or hit a key by mistake
        and saved it.

        i found that if i keep the command line open in windows and run
        saxon after i make changes in the phpedit environment it is blazing
        fast and gives me good tips on where to fix the problems.

        i wont need that other persons code once i get going.
        i am going to make the weather forecast fancier on my site when
        i get the time.

        thanks so much for helping,
        jim


        Comment

        Working...