Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

XSLT Help

  • 04-05-2007 04:43PM
    #1
    Closed Accounts Posts: 461 ✭✭


    Hi all, im trying to formulate a query that is easy to describe but my attempts have not been working so far

    The XML structure looks roughly like
    <Entry>
        <Server>
            <UA>Some value here</UA>
        </Server>
        <Capability>
            <NewCapability name="foo" value="bar"/>
            <MissingCapability name="X">240</MissingCapability>
        <Capability>
    </Entry>
    

    I have no control over this so I cannot re-design the XML layout.

    So what I need to do is print out the value contained in UA iff the name and value of NewCapability match some criteria.

    The first problem I am having is that my <xsl:if> does not seem to be matching the attributes.

    The second problem I am having is that every UA value and every MissingCapability value are printed out, I cannot seem to ignore them even if I explicitly match them.

    My XSL looks roughly like this:
    <xsl:template match="Server">
        </xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="UA">
        <xsl:if test="../Capability/NewCapability[@name]='foo'">
            <xsl:if test="../Capability/NewCapability[@value]='bar'">
                <xsl:value-of select="."/>
                <xsl:apply-templates/>
            </xsl:if>
        </xsl:if>
    </xsl:template>
    
    <xsl:template match="MissingCapability">
        <!-- This keeps printing out the value, how do I ignore -->
        <xsl:apply-templates/>
    </xsl:template>
    

    I've spent a lot of hours over the past two days without making much progress, I could hack a solution together via some string matching in Python but i'd prefer to stick to an XML-based solution.


Comments

  • Registered Users, Registered Users 2 Posts: 151 ✭✭sailorfoley


    match the attributes with XPath using ../elementname/@attributename

    so try : ../Capability/NewCapability/@name='foo'


  • Closed Accounts Posts: 461 ✭✭markf909


    match the attributes with XPath using ../elementname/@attributename

    so try : ../Capability/NewCapability/@name='foo'

    Yup that did it, the test clause just needed @name instead of [@name]

    Thanks :)


  • Registered Users, Registered Users 2 Posts: 151 ✭✭sailorfoley


    No problem. the square brackets you were using are used when you want to do a conditional xpath

    so..... e.g. /bookstore/book[price>35]/title

    This would select all the title nodes with a price node value higher than 35.

    I always find w3schools good for tutorials or if you need to check something quickly.

    http://www.w3schools.com/xpath/default.asp


  • Closed Accounts Posts: 50 ✭✭Farouk.Bulsara


    markf909 wrote:
    The second problem I am having is that every UA value and every MissingCapability value are printed out, I cannot seem to ignore them even if I explicitly match them.

    Mark,

    What does the "full" file look like? Is it something like:

    <Entry>

    <Server>
    <UA>Some value here</UA>
    </Server>
    <Capability>
    <NewCapability name="foo" value="bar"/>
    <MissingCapability name="X">240</MissingCapability>
    </Capability>

    <Server>
    <UA>Firefox</UA>
    </Server>
    <Capability>
    <NewCapability name="foo" value="barbarbar"/>
    <MissingCapability name="X">42</MissingCapability>
    </Capability>

    <Server>
    <UA>Netscape</UA>
    </Server>
    <Capability>
    <NewCapability name="foo" value="notfoo"/>
    <MissingCapability name="X">999</MissingCapability>
    </Capability>

    </Entry>

    Under what circumstances do you NOT want the UA and MissingCapability values printed out?

    Fred


  • Closed Accounts Posts: 461 ✭✭markf909


    Under what circumstances do you NOT want the UA and MissingCapability values printed out?

    I have a large file with a lot of different XSL queries, sometimes I just want to ignore the Missing Capabilites.

    I have that problem sorted now, I simply match any MissingCapabilities node and then just ignore it by not applying templates. Seems to work fine.

    When I posted this problem it was my first foray back into XSL after 4 years, I seem to be in control of things again


  • Advertisement
Advertisement