Fairly advance XSLT question.

Discussion in 'XML' started by Bostonasian, Jan 11, 2007.

  1. Bostonasian

    Bostonasian Guest

    I have following original xml :
    <HealthHistory>
    <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    <BodyFat value="22" TransactionDate="1/2/2007"/>
    <BodyFat value="22" TransactionDate="1/3/2007"/>
    <BodyFat value="22" TransactionDate="1/4/2007"/>
    <HeartRate value="87" TransactionDate="1/1/2007"/>
    <HeartRate value="87" TransactionDate="1/2/2007"/>
    </HealthHistory>

    Then I wrote following xslt :

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput omit-xml-declaration="yes"/>
    <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    <xsl:key name="NameKey" match="*" use="name()"/>
    <xsl:template match="/">
    <EventCalendar>
    <xsl:apply-templates select="//*"/>
    </EventCalendar>
    </xsl:template>

    <xsl:template match="*">
    <xsl:for-each
    select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    <DateKey>
    <xsl:attribute name="Date">
    <xsl:value-of select="@TransactionDate"/>
    </xsl:attribute>
    <xsl:for-each select="key('DateKey',@TransactionDate)">
    <Criteria>
    <xsl:attribute name="Name">
    <xsl:value-of select="name()"/>
    </xsl:attribute>
    </Criteria>
    </xsl:for-each>
    </DateKey>
    </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

    To return following :
    <EventCalendar>
    <DateKey date="1/1/2007">
    <criteria name="BloodPressure"/>
    <criteria name="BloodPressure"/>
    <criteria name="BloodPressure"/>
    <criteria name="HeartRate"/>
    </DateKey>
    <DateKey date="1/2/2007">
    <criteria name="BodyFat"/>
    <criteria name="HeartRate"/>
    </DateKey>
    <DateKey date="1/3/2007">
    <criteria name="BloodPressure"/>
    <criteria name="BodyFat"/>
    </DateKey>
    <DateKey date="1/4/2007">
    <criteria name="BloodPressure"/>
    <criteria name="BodyFat"/>
    </DateKey>
    </EventCalendar>

    But the problem is that I need to return unique value of "name"
    attribute in "criteria" element. Thefore, for <DateKey
    date="1/1/2007">, I want to return <criteria name="BloodPressure"/>
    once, instead of three times.
    What am I missing?
    Thanks bunch
     
    Bostonasian, Jan 11, 2007
    #1
    1. Advertising

  2. You need to read and understand more about grouping. In this case you have
    nested grouping and have to use concatenation of key values in the second
    xsl:key

    The following transformation produces the wanted result:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput omit-xml-declaration="yes"/>
    <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    <xsl:key name="NameKey" match="*"
    use="concat(@TransactionDate, '+', name())"/>
    <xsl:template match="/">
    <EventCalendar>
    <xsl:apply-templates select="/*"/>
    </EventCalendar>
    </xsl:template>
    <xsl:template match="*">
    <xsl:for-each
    select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    <DateKey>
    <xsl:attribute name="Date">
    <xsl:value-of select="@TransactionDate"/>
    </xsl:attribute>
    <xsl:for-each select=
    "key('DateKey',@TransactionDate)
    [generate-id()
    =
    generate-id(key('NameKey',
    concat(@TransactionDate, '+', name())
    )[1])
    ]
    ">
    <Criteria>
    <xsl:attribute name="Name">
    <xsl:value-of select="name()"/>
    </xsl:attribute>
    </Criteria>
    </xsl:for-each>
    </DateKey>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    when applied on the originally provided xml document:

    <HealthHistory>
    <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    <BodyFat value="22" TransactionDate="1/2/2007"/>
    <BodyFat value="22" TransactionDate="1/3/2007"/>
    <BodyFat value="22" TransactionDate="1/4/2007"/>
    <HeartRate value="87" TransactionDate="1/1/2007"/>
    <HeartRate value="87" TransactionDate="1/2/2007"/>
    </HealthHistory>

    the result is:

    <EventCalendar>
    <DateKey Date="1/1/2007">
    <Criteria Name="BloodPressure"/>
    <Criteria Name="HeartRate"/>
    </DateKey>
    <DateKey Date="1/3/2007">
    <Criteria Name="BloodPressure"/>
    <Criteria Name="BodyFat"/>
    </DateKey>
    <DateKey Date="1/4/2007">
    <Criteria Name="BloodPressure"/>
    <Criteria Name="BodyFat"/>
    </DateKey>
    <DateKey Date="1/2/2007">
    <Criteria Name="BodyFat"/>
    <Criteria Name="HeartRate"/>
    </DateKey>
    </EventCalendar>


    Hope this helped.

    Cheers,
    Dimitre Novatchev



    "Bostonasian" <> wrote in message
    news:...
    >I have following original xml :
    > <HealthHistory>
    > <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    > <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    > <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    > <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    > <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    > <BodyFat value="22" TransactionDate="1/2/2007"/>
    > <BodyFat value="22" TransactionDate="1/3/2007"/>
    > <BodyFat value="22" TransactionDate="1/4/2007"/>
    > <HeartRate value="87" TransactionDate="1/1/2007"/>
    > <HeartRate value="87" TransactionDate="1/2/2007"/>
    > </HealthHistory>
    >
    > Then I wrote following xslt :
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput omit-xml-declaration="yes"/>
    > <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    > <xsl:key name="NameKey" match="*" use="name()"/>
    > <xsl:template match="/">
    > <EventCalendar>
    > <xsl:apply-templates select="//*"/>
    > </EventCalendar>
    > </xsl:template>
    >
    > <xsl:template match="*">
    > <xsl:for-each
    > select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    > <DateKey>
    > <xsl:attribute name="Date">
    > <xsl:value-of select="@TransactionDate"/>
    > </xsl:attribute>
    > <xsl:for-each select="key('DateKey',@TransactionDate)">
    > <Criteria>
    > <xsl:attribute name="Name">
    > <xsl:value-of select="name()"/>
    > </xsl:attribute>
    > </Criteria>
    > </xsl:for-each>
    > </DateKey>
    > </xsl:for-each>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    >
    > To return following :
    > <EventCalendar>
    > <DateKey date="1/1/2007">
    > <criteria name="BloodPressure"/>
    > <criteria name="BloodPressure"/>
    > <criteria name="BloodPressure"/>
    > <criteria name="HeartRate"/>
    > </DateKey>
    > <DateKey date="1/2/2007">
    > <criteria name="BodyFat"/>
    > <criteria name="HeartRate"/>
    > </DateKey>
    > <DateKey date="1/3/2007">
    > <criteria name="BloodPressure"/>
    > <criteria name="BodyFat"/>
    > </DateKey>
    > <DateKey date="1/4/2007">
    > <criteria name="BloodPressure"/>
    > <criteria name="BodyFat"/>
    > </DateKey>
    > </EventCalendar>
    >
    > But the problem is that I need to return unique value of "name"
    > attribute in "criteria" element. Thefore, for <DateKey
    > date="1/1/2007">, I want to return <criteria name="BloodPressure"/>
    > once, instead of three times.
    > What am I missing?
    > Thanks bunch
    >
     
    Dimitre Novatchev, Jan 12, 2007
    #2
    1. Advertising

  3. Bostonasian

    Myron Turner Guest

    Dimitre Novatchev wrote:
    > You need to read and understand more about grouping. In this case you have
    > nested grouping and have to use concatenation of key values in the second
    > xsl:key
    >
    > The following transformation produces the wanted result:
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput omit-xml-declaration="yes"/>
    > <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    > <xsl:key name="NameKey" match="*"
    > use="concat(@TransactionDate, '+', name())"/>
    > <xsl:template match="/">
    > <EventCalendar>
    > <xsl:apply-templates select="/*"/>
    > </EventCalendar>
    > </xsl:template>
    > <xsl:template match="*">
    > <xsl:for-each
    > select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    > <DateKey>
    > <xsl:attribute name="Date">
    > <xsl:value-of select="@TransactionDate"/>
    > </xsl:attribute>
    > <xsl:for-each select=
    > "key('DateKey',@TransactionDate)
    > [generate-id()
    > =
    > generate-id(key('NameKey',
    > concat(@TransactionDate, '+', name())
    > )[1])
    > ]
    > ">
    > <Criteria>
    > <xsl:attribute name="Name">
    > <xsl:value-of select="name()"/>
    > </xsl:attribute>
    > </Criteria>
    > </xsl:for-each>
    > </DateKey>
    > </xsl:for-each>
    > </xsl:template>
    > </xsl:stylesheet>
    >
    > when applied on the originally provided xml document:
    >
    > <HealthHistory>
    > <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    > <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    > <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    > <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    > <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    > <BodyFat value="22" TransactionDate="1/2/2007"/>
    > <BodyFat value="22" TransactionDate="1/3/2007"/>
    > <BodyFat value="22" TransactionDate="1/4/2007"/>
    > <HeartRate value="87" TransactionDate="1/1/2007"/>
    > <HeartRate value="87" TransactionDate="1/2/2007"/>
    > </HealthHistory>
    >
    > the result is:
    >
    > <EventCalendar>
    > <DateKey Date="1/1/2007">
    > <Criteria Name="BloodPressure"/>
    > <Criteria Name="HeartRate"/>
    > </DateKey>
    > <DateKey Date="1/3/2007">
    > <Criteria Name="BloodPressure"/>
    > <Criteria Name="BodyFat"/>
    > </DateKey>
    > <DateKey Date="1/4/2007">
    > <Criteria Name="BloodPressure"/>
    > <Criteria Name="BodyFat"/>
    > </DateKey>
    > <DateKey Date="1/2/2007">
    > <Criteria Name="BodyFat"/>
    > <Criteria Name="HeartRate"/>
    > </DateKey>
    > </EventCalendar>
    >
    >
    > Hope this helped.
    >
    > Cheers,
    > Dimitre Novatchev
    >
    >
    >
    > "Bostonasian" <> wrote in message
    > news:...
    >> I have following original xml :
    >> <HealthHistory>
    >> <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    >> <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    >> <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    >> <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    >> <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    >> <BodyFat value="22" TransactionDate="1/2/2007"/>
    >> <BodyFat value="22" TransactionDate="1/3/2007"/>
    >> <BodyFat value="22" TransactionDate="1/4/2007"/>
    >> <HeartRate value="87" TransactionDate="1/1/2007"/>
    >> <HeartRate value="87" TransactionDate="1/2/2007"/>
    >> </HealthHistory>
    >>
    >> Then I wrote following xslt :
    >>
    >> <xsl:stylesheet version="1.0"
    >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >> <xsl:eek:utput omit-xml-declaration="yes"/>
    >> <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    >> <xsl:key name="NameKey" match="*" use="name()"/>
    >> <xsl:template match="/">
    >> <EventCalendar>
    >> <xsl:apply-templates select="//*"/>
    >> </EventCalendar>
    >> </xsl:template>
    >>
    >> <xsl:template match="*">
    >> <xsl:for-each
    >> select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    >> <DateKey>
    >> <xsl:attribute name="Date">
    >> <xsl:value-of select="@TransactionDate"/>
    >> </xsl:attribute>
    >> <xsl:for-each select="key('DateKey',@TransactionDate)">
    >> <Criteria>
    >> <xsl:attribute name="Name">
    >> <xsl:value-of select="name()"/>
    >> </xsl:attribute>
    >> </Criteria>
    >> </xsl:for-each>
    >> </DateKey>
    >> </xsl:for-each>
    >> </xsl:template>
    >>
    >> </xsl:stylesheet>
    >>
    >> To return following :
    >> <EventCalendar>
    >> <DateKey date="1/1/2007">
    >> <criteria name="BloodPressure"/>
    >> <criteria name="BloodPressure"/>
    >> <criteria name="BloodPressure"/>
    >> <criteria name="HeartRate"/>
    >> </DateKey>
    >> <DateKey date="1/2/2007">
    >> <criteria name="BodyFat"/>
    >> <criteria name="HeartRate"/>
    >> </DateKey>
    >> <DateKey date="1/3/2007">
    >> <criteria name="BloodPressure"/>
    >> <criteria name="BodyFat"/>
    >> </DateKey>
    >> <DateKey date="1/4/2007">
    >> <criteria name="BloodPressure"/>
    >> <criteria name="BodyFat"/>
    >> </DateKey>
    >> </EventCalendar>
    >>
    >> But the problem is that I need to return unique value of "name"
    >> attribute in "criteria" element. Thefore, for <DateKey
    >> date="1/1/2007">, I want to return <criteria name="BloodPressure"/>
    >> once, instead of three times.
    >> What am I missing?
    >> Thanks bunch
    >>

    >
    >

    If you use PHP, you can download XML_PullParser from
    http://www.mturner.org/XML_PullParser/
    and use a script such as this:

    <?php
    require_once "XML_PullParser/XML_PullParser.inc";
    $doc = <<<DOCUMENT
    <HealthHistory>
    <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    <BodyFat value="22" TransactionDate="1/2/2007"/>
    <BodyFat value="22" TransactionDate="1/3/2007"/>
    <BodyFat value="22" TransactionDate="1/4/2007"/>
    <HeartRate value="87" TransactionDate="1/1/2007"/>
    <HeartRate value="87" TransactionDate="1/2/2007"/>
    </HealthHistory>
    DOCUMENT;

    XML_PullParser_caseSensitive(TRUE);
    $tags = array("HealthHistory");
    $child_tags = array();

    $parser = new XML_PullParser_doc($doc,$tags,$child_tags);

    $parser->XML_PullParser_getToken();
    $attrs = $parser->XML_PullParser_setAttrLoop();

    while($attr_loop = $parser->XML_PullParser_nextAttr()) {
    // $attr_loop[0] is name of element
    // $attr_loop[1] is array of atttributes for element
    $date = $attr_loop[1]['TransactionDate'];
    $output[$date][$attr_loop[0]] = true;
    }


    $output = array();

    echo "<EventCalendar>\n";

    foreach($output as $DateKey=>$criteria) {
    echo " <DateKey date = \"$DateKey\">\n";
    foreach(array_keys($criteria) as $name) {
    echo " <criteria name = \"$name\" />\n";
    }
    echo " </DateKey>\n";
    }

    echo "</EventCalendar>\n";

    ?>

    --

    _____________________
    Myron Turner
    http://www.room535.org
    http://www.bstatzero.org
    http://www.mturner.org/XML_PullParser/
     
    Myron Turner, Jan 12, 2007
    #3
  4. Bostonasian

    Bostonasian Guest

    Thank you very much

    Myron Turner wrote:
    > Dimitre Novatchev wrote:
    > > You need to read and understand more about grouping. In this case you have
    > > nested grouping and have to use concatenation of key values in the second
    > > xsl:key
    > >
    > > The following transformation produces the wanted result:
    > >
    > > <xsl:stylesheet version="1.0"
    > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > > <xsl:eek:utput omit-xml-declaration="yes"/>
    > > <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    > > <xsl:key name="NameKey" match="*"
    > > use="concat(@TransactionDate, '+', name())"/>
    > > <xsl:template match="/">
    > > <EventCalendar>
    > > <xsl:apply-templates select="/*"/>
    > > </EventCalendar>
    > > </xsl:template>
    > > <xsl:template match="*">
    > > <xsl:for-each
    > > select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    > > <DateKey>
    > > <xsl:attribute name="Date">
    > > <xsl:value-of select="@TransactionDate"/>
    > > </xsl:attribute>
    > > <xsl:for-each select=
    > > "key('DateKey',@TransactionDate)
    > > [generate-id()
    > > =
    > > generate-id(key('NameKey',
    > > concat(@TransactionDate, '+', name())
    > > )[1])
    > > ]
    > > ">
    > > <Criteria>
    > > <xsl:attribute name="Name">
    > > <xsl:value-of select="name()"/>
    > > </xsl:attribute>
    > > </Criteria>
    > > </xsl:for-each>
    > > </DateKey>
    > > </xsl:for-each>
    > > </xsl:template>
    > > </xsl:stylesheet>
    > >
    > > when applied on the originally provided xml document:
    > >
    > > <HealthHistory>
    > > <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    > > <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    > > <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    > > <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    > > <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    > > <BodyFat value="22" TransactionDate="1/2/2007"/>
    > > <BodyFat value="22" TransactionDate="1/3/2007"/>
    > > <BodyFat value="22" TransactionDate="1/4/2007"/>
    > > <HeartRate value="87" TransactionDate="1/1/2007"/>
    > > <HeartRate value="87" TransactionDate="1/2/2007"/>
    > > </HealthHistory>
    > >
    > > the result is:
    > >
    > > <EventCalendar>
    > > <DateKey Date="1/1/2007">
    > > <Criteria Name="BloodPressure"/>
    > > <Criteria Name="HeartRate"/>
    > > </DateKey>
    > > <DateKey Date="1/3/2007">
    > > <Criteria Name="BloodPressure"/>
    > > <Criteria Name="BodyFat"/>
    > > </DateKey>
    > > <DateKey Date="1/4/2007">
    > > <Criteria Name="BloodPressure"/>
    > > <Criteria Name="BodyFat"/>
    > > </DateKey>
    > > <DateKey Date="1/2/2007">
    > > <Criteria Name="BodyFat"/>
    > > <Criteria Name="HeartRate"/>
    > > </DateKey>
    > > </EventCalendar>
    > >
    > >
    > > Hope this helped.
    > >
    > > Cheers,
    > > Dimitre Novatchev
    > >
    > >
    > >
    > > "Bostonasian" <> wrote in message
    > > news:...
    > >> I have following original xml :
    > >> <HealthHistory>
    > >> <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    > >> <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    > >> <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    > >> <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    > >> <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    > >> <BodyFat value="22" TransactionDate="1/2/2007"/>
    > >> <BodyFat value="22" TransactionDate="1/3/2007"/>
    > >> <BodyFat value="22" TransactionDate="1/4/2007"/>
    > >> <HeartRate value="87" TransactionDate="1/1/2007"/>
    > >> <HeartRate value="87" TransactionDate="1/2/2007"/>
    > >> </HealthHistory>
    > >>
    > >> Then I wrote following xslt :
    > >>
    > >> <xsl:stylesheet version="1.0"
    > >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > >> <xsl:eek:utput omit-xml-declaration="yes"/>
    > >> <xsl:key name="DateKey" match="*" use="@TransactionDate"/>
    > >> <xsl:key name="NameKey" match="*" use="name()"/>
    > >> <xsl:template match="/">
    > >> <EventCalendar>
    > >> <xsl:apply-templates select="//*"/>
    > >> </EventCalendar>
    > >> </xsl:template>
    > >>
    > >> <xsl:template match="*">
    > >> <xsl:for-each
    > >> select="*[generate-id()=generate-id(key('DateKey',@TransactionDate)[1])]">
    > >> <DateKey>
    > >> <xsl:attribute name="Date">
    > >> <xsl:value-of select="@TransactionDate"/>
    > >> </xsl:attribute>
    > >> <xsl:for-each select="key('DateKey',@TransactionDate)">
    > >> <Criteria>
    > >> <xsl:attribute name="Name">
    > >> <xsl:value-of select="name()"/>
    > >> </xsl:attribute>
    > >> </Criteria>
    > >> </xsl:for-each>
    > >> </DateKey>
    > >> </xsl:for-each>
    > >> </xsl:template>
    > >>
    > >> </xsl:stylesheet>
    > >>
    > >> To return following :
    > >> <EventCalendar>
    > >> <DateKey date="1/1/2007">
    > >> <criteria name="BloodPressure"/>
    > >> <criteria name="BloodPressure"/>
    > >> <criteria name="BloodPressure"/>
    > >> <criteria name="HeartRate"/>
    > >> </DateKey>
    > >> <DateKey date="1/2/2007">
    > >> <criteria name="BodyFat"/>
    > >> <criteria name="HeartRate"/>
    > >> </DateKey>
    > >> <DateKey date="1/3/2007">
    > >> <criteria name="BloodPressure"/>
    > >> <criteria name="BodyFat"/>
    > >> </DateKey>
    > >> <DateKey date="1/4/2007">
    > >> <criteria name="BloodPressure"/>
    > >> <criteria name="BodyFat"/>
    > >> </DateKey>
    > >> </EventCalendar>
    > >>
    > >> But the problem is that I need to return unique value of "name"
    > >> attribute in "criteria" element. Thefore, for <DateKey
    > >> date="1/1/2007">, I want to return <criteria name="BloodPressure"/>
    > >> once, instead of three times.
    > >> What am I missing?
    > >> Thanks bunch
    > >>

    > >
    > >

    > If you use PHP, you can download XML_PullParser from
    > http://www.mturner.org/XML_PullParser/
    > and use a script such as this:
    >
    > <?php
    > require_once "XML_PullParser/XML_PullParser.inc";
    > $doc = <<<DOCUMENT
    > <HealthHistory>
    > <BloodPressure low="80" high="120" TransactionDate="1/1/2007"/>
    > <BloodPressure low="90" high="140" TransactionDate="1/1/2007"/>
    > <BloodPressure low="70" high="110" TransactionDate="1/1/2007"/>
    > <BloodPressure low="80" high="120" TransactionDate="1/3/2007"/>
    > <BloodPressure low="80" high="120" TransactionDate="1/4/2007"/>
    > <BodyFat value="22" TransactionDate="1/2/2007"/>
    > <BodyFat value="22" TransactionDate="1/3/2007"/>
    > <BodyFat value="22" TransactionDate="1/4/2007"/>
    > <HeartRate value="87" TransactionDate="1/1/2007"/>
    > <HeartRate value="87" TransactionDate="1/2/2007"/>
    > </HealthHistory>
    > DOCUMENT;
    >
    > XML_PullParser_caseSensitive(TRUE);
    > $tags = array("HealthHistory");
    > $child_tags = array();
    >
    > $parser = new XML_PullParser_doc($doc,$tags,$child_tags);
    >
    > $parser->XML_PullParser_getToken();
    > $attrs = $parser->XML_PullParser_setAttrLoop();
    >
    > while($attr_loop = $parser->XML_PullParser_nextAttr()) {
    > // $attr_loop[0] is name of element
    > // $attr_loop[1] is array of atttributes for element
    > $date = $attr_loop[1]['TransactionDate'];
    > $output[$date][$attr_loop[0]] = true;
    > }
    >
    >
    > $output = array();
    >
    > echo "<EventCalendar>\n";
    >
    > foreach($output as $DateKey=>$criteria) {
    > echo " <DateKey date = \"$DateKey\">\n";
    > foreach(array_keys($criteria) as $name) {
    > echo " <criteria name = \"$name\" />\n";
    > }
    > echo " </DateKey>\n";
    > }
    >
    > echo "</EventCalendar>\n";
    >
    > ?>
    >
    > --
    >
    > _____________________
    > Myron Turner
    > http://www.room535.org
    > http://www.bstatzero.org
    > http://www.mturner.org/XML_PullParser/
     
    Bostonasian, Jan 12, 2007
    #4
    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:
    13
    Views:
    751
    Chaddy2222
    Apr 11, 2006
  2. Replies:
    5
    Views:
    412
    JEDIDIAH
    Jun 20, 2006
  3. Erik Max Francis
    Replies:
    0
    Views:
    349
    Erik Max Francis
    Oct 6, 2003
  4. Mark
    Replies:
    2
    Views:
    326
    Moritz Beller
    Aug 18, 2005
  5. Steckly, Ron

    Fairly elementary UNIX question

    Steckly, Ron, Dec 23, 2007, in forum: Ruby
    Replies:
    5
    Views:
    142
    M. Edward (Ed) Borasky
    Dec 23, 2007
Loading...

Share This Page