Pick/Choose XML from XSL

Discussion in 'XML' started by Prabh, Dec 1, 2004.

  1. Prabh

    Prabh Guest

    Hello all,
    I want to HTML-ize an .xml (Test.xml) file and am running into a
    problem with my XSL file.

    The .xml contains info about each tag, called 'targets' and each
    target has a message with certain priority. I want to bundle all these
    targets in a top-area which have "a href" links to the details section
    down below. Also, I want to ignore messages with 'debug' priority and
    print only the 'info' priority messages.


    Test.xml:
    ==========================X M L==================================
    <?xml version="1.0" encoding="UTF-8" ?>
    <?xml-stylesheet type="text/xsl" href="Test.xsl"?>

    <main>
    <target name="TargetOne">
    <task>
    <message priority="info"><![CDATA[Info about Target One]]></message>
    </task>
    </target>

    <target name="TargetTwo">
    <task>
    <message priority="debug"><![CDATA[Info about Target Two: Ideally I
    shouldnt be displayed!!]]></message>
    </task>
    </target>

    <target name="TargetThree">
    <task>
    <message priority="info"><![CDATA[Info about Target Three]]>
    </message>
    </task>
    </target>

    </main>

    ==========================X M L==================================



    I've gotten this far with my .xsl:

    ==========================X S L==================================

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

    <xsl:template match="main">
    <html>
    <body>

    <!-- make header links -->
    <xsl:for-each select="target">
    <xsl:if test="@name='TargetOne'
    or @name='TargetTwo'
    or @name='TargetThree'">
    <a href="#{generate-id()}"> <xsl:value-of select="@name"/>
    </a>
    <br/>
    </xsl:if>
    </xsl:for-each>

    <hr/>
    <!-- show the targets, tasks and messages -->
    <xsl:apply-templates select="target"/>

    </body>
    </html>

    </xsl:template>


    <xsl:template match="target">
    <xsl:if test="@name='TargetOne'
    or @name='TargetTwo'
    or @name='TargetThree'">
    <a name="{generate-id()}"/>
    </xsl:if>

    <h4>
    <xsl:text>Target: </xsl:text>
    <xsl:value-of select="@name"/>
    </h4>

    <!-- show the messages -->
    <xsl:apply-templates select="task"/>
    </xsl:template>

    </xsl:stylesheet>
    ==========================X S L==================================

    I can now print the desired HTML with targets bundled up in a top-area
    with links to details down below. But, I'm unable to ignore/discard
    the 'debug' messages. I'm trying to use '<xsl:template
    match="message[@priority] != 'debug'
    but it doesn seem to work, the Target Two is always being printed.

    Could anyone give me pointers on how to go about this, please?

    Thanks for your time,
    Prabh
    Prabh, Dec 1, 2004
    #1
    1. Advertising

  2. On 1 Dec 2004 14:03:14 -0800, (Prabh) wrote:
    ....
    > Also, I want to ignore messages with 'debug' priority and
    >print only the 'info' priority messages.
    >
    >
    >Test.xml:
    >==========================X M L==================================

    ....
    > <task>
    > <message priority="debug"><![CDATA[Info about Target Two: Ideally I
    >shouldnt be displayed!!]]></message>
    > </task>

    ....
    > <!-- show the messages -->
    > <xsl:apply-templates select="task"/>

    ....
    >I'm unable to ignore/discard the 'debug' messages.
    > I'm trying to use '<xsl:template
    >match="message[@priority] != 'debug'


    You could use a few different approaches:

    <xsl:apply-templates select="task[message/@priority != 'debug']"/>
    to apply templates only to those tasks with the right priority,
    if <message> is the only possible child element of <task>, or

    or

    <xsl:template match="task">
    <xsl:if test="message/@priority != 'debug'">
    <xsl:value-of select="message"/>
    </xsl:if>
    <!-- ... other processing for contents of task ... -->
    </xsl:template>

    or even

    <xsl:template match="message">
    <xsl:if test="@priority != 'debug'><xsl:value-of select="."/></xsl:if>
    </xsl:template>

    --
    Morris M. Keesan --
    Morris M. Keesan, Dec 2, 2004
    #2
    1. Advertising

  3. Prabh wrote:
    > Hello all,
    > I want to HTML-ize an .xml (Test.xml) file and am running into a
    > problem with my XSL file.
    >
    > The .xml contains info about each tag, called 'targets' and each
    > target has a message with certain priority. I want to bundle all these
    > targets in a top-area which have "a href" links to the details section
    > down below. Also, I want to ignore messages with 'debug' priority and
    > print only the 'info' priority messages.
    >
    >
    > Test.xml:
    > ==========================X M L==================================
    > <?xml version="1.0" encoding="UTF-8" ?>
    > <?xml-stylesheet type="text/xsl" href="Test.xsl"?>
    >
    > <main>
    > <target name="TargetOne">
    > <task>
    > <message priority="info"><![CDATA[Info about Target One]]></message>
    > </task>
    > </target>
    >
    > <target name="TargetTwo">
    > <task>
    > <message priority="debug"><![CDATA[Info about Target Two: Ideally I
    > shouldnt be displayed!!]]></message>
    > </task>
    > </target>
    >
    > <target name="TargetThree">
    > <task>
    > <message priority="info"><![CDATA[Info about Target Three]]>
    > </message>
    > </task>
    > </target>
    >
    > </main>
    >
    > ==========================X M L==================================
    >
    >
    >
    > I've gotten this far with my .xsl:
    >
    > ==========================X S L==================================
    >
    > <?xml version="1.0"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    > <xsl:eek:utput method="html" indent="yes"/>
    >
    > <xsl:template match="main">
    > <html>
    > <body>
    >
    > <!-- make header links -->
    > <xsl:for-each select="target">
    > <xsl:if test="@name='TargetOne'
    > or @name='TargetTwo'
    > or @name='TargetThree'">
    > <a href="#{generate-id()}"> <xsl:value-of select="@name"/>
    > </a>
    > <br/>
    > </xsl:if>
    > </xsl:for-each>
    >
    > <hr/>
    > <!-- show the targets, tasks and messages -->
    > <xsl:apply-templates select="target"/>
    >
    > </body>
    > </html>
    >
    > </xsl:template>
    >
    >
    > <xsl:template match="target">
    > <xsl:if test="@name='TargetOne'
    > or @name='TargetTwo'
    > or @name='TargetThree'">
    > <a name="{generate-id()}"/>
    > </xsl:if>
    >
    > <h4>
    > <xsl:text>Target: </xsl:text>
    > <xsl:value-of select="@name"/>
    > </h4>
    >
    > <!-- show the messages -->
    > <xsl:apply-templates select="task"/>
    > </xsl:template>
    >
    > </xsl:stylesheet>
    > ==========================X S L==================================
    >
    > I can now print the desired HTML with targets bundled up in a top-area
    > with links to details down below. But, I'm unable to ignore/discard
    > the 'debug' messages. I'm trying to use '<xsl:template
    > match="message[@priority] != 'debug'
    > but it doesn seem to work, the Target Two is always being printed.
    >
    > Could anyone give me pointers on how to go about this, please?


    You're nearly there! The test for equality needs to be inside the predicate.

    <xsl:template match="message[@priority != 'debug']">
    </...>

    You should use that in both the header link section, and the content
    section.

    Are your generated IDs linking up? the generate-id() function produces a
    different value each time it is called. The one in your header won't
    match the one in the content section! I'd use the @name attribute of the
    <target> tag as the name to reference.

    Nige
    ExGuardianReader, Dec 4, 2004
    #3
  4. On Sat, 4 Dec 2004 19:13:10 +0000 (UTC), ExGuardianReader
    <> wrote:
    ....
    >Are your generated IDs linking up? the generate-id() function produces a
    >different value each time it is called. The one in your header won't
    >match the one in the content section!


    Nope. For a single XSLT run, the id returned must be the same every
    time the generate-id() function is called for the same node. But
    there's some confusion about this because of the second part of the
    following, from <http://www.w3.org/TR/xslt#function-generate-id>:

    "The generate-id function ... always generates the same identifier for
    the same node and ... different identifiers are always generated from
    different nodes. An implementation is under no obligation to generate
    the same identifiers each time a document is transformed."
    --
    Morris M. Keesan --
    Morris M. Keesan, Dec 4, 2004
    #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. Ruthless

    xsl:choose

    Ruthless, Dec 26, 2003, in forum: XML
    Replies:
    2
    Views:
    456
    Ruthless
    Dec 27, 2003
  2. Replies:
    1
    Views:
    3,596
    A. Bolmarcich
    May 27, 2005
  3. freefly_xml
    Replies:
    4
    Views:
    845
    freefly_xml
    May 16, 2006
  4. tom c
    Replies:
    5
    Views:
    391
    tom c
    Nov 1, 2006
  5. michael75
    Replies:
    0
    Views:
    1,831
    michael75
    Aug 3, 2010
Loading...

Share This Page