data inside the xml in order to match a template

Discussion in 'XML' started by bjam, Apr 6, 2005.

  1. bjam

    bjam Guest

    Hi, I am trying to have a style sheet read an xml file that has some
    tagged data inside of it that has the name of the section the syle
    sheet should process, does anyone know how I can do this?? Thanks
    greatly for any help, I really appreciate it. Below is an example of
    what I am describing.

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <?xml-stylesheet type="text/xsl" href="../xml/dqm_general.dev.xsl"?>

    <perldata>
    <arguments>

    <test>TEST 0003</test>

    <file>capacity_count.xml</file>

    <xsl>dqm_general.dev.xsl</xsl>
    </arguments>

    Now further on in the xml document there is a tagged section called
    item with a key value = "TEST 003" in the xml as such below


    <item key="TEST 0003"> ... </item>

    I want the style sheet to be able to read the <argument>/<test> tag in
    order to know which section to match / process. I basically want the
    template match and select commands to use this TEST 003 value at
    processing time. Any ideas? Thanks again for your help with this.

    <!-- apply templates for the test found in test tag -->

    <xsl:apply-templates select='hashref/item @key="{arguments/test}"'/>

    <!-- test template to process -->

    <xsl:template match='hashref/item[@key={arguments/test}]'>
     
    bjam, Apr 6, 2005
    #1
    1. Advertising

  2. several ways, eg:

    <!-- throw items away by default -->
    <xsl:template match="item"/>

    <!--don't throw this one away -->
    <xsl:template match="item[@key=/perldata/arguments/test]">
    do something with <xsl:value-of select="@key"/>
    </xsl:template>

    David
     
    David Carlisle, Apr 6, 2005
    #2
    1. Advertising

  3. David,

    Thank you so much! So simple, but was missing just not using quotes!
    Thanks again as I was venturing in the <xsl:variable> tag world which
    did not seem to work for me.. I was using the following.. maybe you can
    comment on why this was not working? Eitherway I really appreciate the
    help again. Your the best!

    <xsl:template match="perldata">

    <xsl:variable name="_test"
    select="/perldata/arguments/test"/>

    <html>
    <basefont face="Verdana" size="2" />
    <body>
    <xsl:apply-templates select='hashref/item[@key="$_test"]'/>

    </body>
    </html>
    </xsl:template>

    <!-- template match -->

    <xsl:template match='hashref/item[@key="$_test"]'>







    *** Sent via Developersdex http://www.developersdex.com ***

    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Benjamin Samim, Apr 6, 2005
    #3
  4. @key="$_test"

    that tests if the key attribute is equal to the string $_test

    You want to test if it is equal to the variable $_test so

    @key=$_test

    <!-- template match -->

    <xsl:template match='hashref/item[@key="$_test"]'>

    This is similar but you can't just remove the " here as you can't use a
    variable in an XSLT1 match pattern.

    But since you have already filtered the apply-templates select you can
    just have

    <xsl:template match='hashref/item">

    David
     
    David Carlisle, Apr 6, 2005
    #4
  5. bjam

    bjam Guest

    Thanks again david. If you wanted to be able to match using a regular
    expression your key value how could you do that? I have used
    starts-with before
    but say I have the following and I want to filter only SUMMARY sections
    that have the item key="e_result" value of true (see below). Is there
    a good way to do this if say the key may not always be e_result but
    could be g_result?

    <item key="SUMMARY">
    <hashref memory_address="0x8db9dec">
    <item key="non_viewable">
    <hashref memory_address="0x8e46cd4">
    <item key="test_value">TEST+0003</item>
    </hashref>
    </item>
    <item key="viewable">
    <hashref memory_address="0x8e511a0">
    <item key="a_name">RBCNCN-0855-CITG</item>
    <item key="b_capacity">3910</item>
    <item key="c_lower">4297.7</item>
    <item key="d_upper">3501.9</item>
    <item key="e_result">true</item>
    </hashref>
    </item>
    </hashref>
    </item>
    </hashref>
    </item>

    so my template section would be as follows

    <!-- syntax is wrong here, but concept is shown of what I am trying to
    do -->
    <xsl:template
    match="//item[@key='SUMMARY']/key(matches(_result))='true'">
     
    bjam, Apr 6, 2005
    #5

  6. > Thanks again david. If you wanted to be able to match using a regular


    You are using XSLT2 draft? You could have said. XSLT as currently
    specified doesn't have regular expressions at all. If you are using
    xslt2 then the comment about variables not allowed in match patterns
    doesn't apply.


    > <xsl:template
    > match="//item[@key='SUMMARY']/key(matches(_result))='true'">



    There is never any point in starting a match pattern with // it never
    does anytthing useful. It sometimes changes the default priority but
    here it does nothing.

    <xsl:template
    match="item[@key='SUMMARY']//item[matches(@key,'_result')]'">

    or in xslt1

    <xsl:template
    match="item[@key='SUMMARY']//item[contains(@key,'_result')]'">

    as contains is equivalent to matches for this trivial regex of a fixed
    string.

    David
     
    David Carlisle, Apr 6, 2005
    #6
  7. bjam

    bjam Guest

    I am using IE 6.0 's built in XSLT translation is this considered xslt1
    or xslt2?
     
    bjam, Apr 8, 2005
    #7
  8. xslt1
     
    David Carlisle, Apr 8, 2005
    #8
    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. blueblueblue2005
    Replies:
    6
    Views:
    1,029
    blueblueblue2005
    Jul 19, 2005
  2. Alex Buell
    Replies:
    2
    Views:
    685
    Alex Buell
    Feb 21, 2006
  3. Joseph Turian
    Replies:
    2
    Views:
    894
    Joseph Turian
    Mar 22, 2007
  4. Replies:
    10
    Views:
    1,088
    Peter Flynn
    Sep 14, 2007
  5. abir
    Replies:
    4
    Views:
    418
Loading...

Share This Page