Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doing a lookup using a 2nd XML document. 1

Status
Not open for further replies.

ricka

Programmer
Dec 7, 2000
5
GB
I'm trying to perform a lookup to a 2nd XML document in order to convert a code to a real value, but I can't find any examples that work with my file format.

I have tutorial.xml

<tutorial>
<author idref="1"/>
</tutorial>

.. and author.xml

<authors>
<author id="1" word="one">
<name>foo</name>
</author>
<author id="2" word="two">
<name>bar</name>
</author>
</authors>

I have the following section in my XSL and am able to return the value 'foo'...

<xsl:template match="author">
<xsl:copy>
<xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]/name"/>
</xsl:copy>
</xsl:template>


BUT.. really I want to return 'one', but I can't figure out the syntax required to use the @word attribute.

Can anyone help?
 
Not sure if I get the idea.
[tt]
<xsl:template match="author">
<xsl:copy>
[blue]<xsl:attribute name="word">
<xsl:value-of select="document('author.xml')/authors/author[@id =current()/@idref]/@word"/>
</xsl:attribute>[/blue]
<xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]/name"/>
</xsl:copy>
</xsl:template>
[/tt]
 
Why not just:
Code:
<xsl:template match="author">
    <xsl:copy>
        <xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]/@word"/>
    </xsl:copy>
</xsl:template>

Jon

"I don't regret this, but I both rue and lament it.
 
Funny you should suggest that cos it was the first thing I tried was something similar....

<xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]@word"/>

This threw up errors, so I have now tried your suggestions and it returns nothing because /@word would be looking for an attribute from the names tag (not the author tag).

Maybe this function wont do it!!
 
Nope, "author/@word" will match the word attribute on the author node. To match a word attribute on the name tag, you would do "author/name/@word"

Post your actual XML, XSL and we can see where the problem is.

Jon

"I don't regret this, but I both rue and lament it.
 
My version gives this.
[tt]
<author word="one">
<name>foo</name>
</author>
[/tt]
If that's not what you want, say it.
 
You are correct, author/@word should give me the word attribute but my example is still returning nothing (however it also doesnt return any errors)

Here's my files....

tutorial.xml
Code:
<tutorial>
    <author idref="1"/>
</tutorial>

author.xml
Code:
<authors>
    <author id="1" word="one">
        <name>foo</name>
    </author>
    <author id="2" word="two">
        <name>bar</name>
    </author>
</authors>

test.xsl
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:fo="[URL unfurl="true"]http://www.w3.org/1999/XSL/Format">[/URL]

<xsl:template match="author">
    <xsl:copy>
        <xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]/name"/>        
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Thanks for the help so far guys.
 
But you still do not say what output you are anticipating!

A way to get nothing on word is this, using my renderment. Note the context node change. Maybe you have something like it as you keep saying returning nothing.
[tt]
<xsl:template match="author">
<xsl:copy>
<xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]/name"/>
</xsl:copy>
<!-- simulate null result for illustration -->
<xsl:attribute name="word">
<xsl:value-of select="document('author.xml')/authors/author[@id =current()/@idref]/@word"/>
</xsl:attribute>
</xsl:template>
[/tt]
See the difference?
 
Amendment:

I meant to paste the block within copy element. Here is a retake.
[tt]
<xsl:template match="author">
<xsl:copy>
<xsl:copy-of select="document('author.xml')/authors/author[@id =current()/@idref]/name"/>
<!-- simulate null result for illustration -->
<xsl:attribute name="word">
<xsl:value-of select="document('author.xml')/authors/author[@id =current()/@idref]/@word"/>
</xsl:attribute>
</xsl:copy>
</xsl:template>
[/tt]
 
Sorry for being a little vague,.. I am expecting 'one' to be displayed.
 
But to be vague is consuming more energy than to be precise. To be vague, you try your best in twisting and bending words to fool the forum. Why can't you give a sample output? in xml? in text? ... (or you do not know those notions?!)

Here is one to get xml output
[tt]
<author>one</author>
[/tt]
with the template like this.
[tt]
<xsl:template match="author">
<xsl:copy>
<xsl:value-of select="document('author.xml')/authors/author[@id =current()/@idref]/@word"/>
</xsl:copy>
</xsl:template>
[/tt]
But you said retrieving nothing.
 
I think we have a classic case of a communication breakdown, I really dont understand what you are asking now, but when I say I'm getting 'nothing' then my browser shows an empty white screen.

Obviously I'm not trying to be vague, but it's not always easy to see things from other peoples point of view when you've already wasted so much time trying to get it to work already. So go easy on me :)

I'm starting to think that this method just doesn't work for attributes, (unless it works on your PC) so I'll probably look for a completely different way of getting around the problem.

Is it something wrong with my browser maybe? (I'm using IE6.0 sp2)
 
>I'm starting to think that this method just doesn't work for attributes
If that touches the matter of objective functionality, I can't help to assure you _no_. It works for attributes as well as anything else.

>so I'll probably look for a completely different way of getting around the problem.
If that your will, you certain can work on it.
 
I think I see what your problem is.

You are using copy.

In your first example you have:
Code:
<xsl:template match="author">
  <xsl:copy>
    <xsl:value-of select="document('author.xml')/authors/author[@id =current()/@idref]/name"/>
  </xsl:copy>
</xsl:template>
Which returns
Code:
<author><name>foo</name></author>
So you see simply "foo" in the browser.

In my example:
Code:
<xsl:template match="author">
  <xsl:copy>
    <xsl:value-of select="document('author.xml')/authors/author[@id =current()/@idref]/@word"/>
  </xsl:copy>
</xsl:template>
It returns:
Code:
<author word="one"/>
So nothing displays in browser. If you click view source, you will see (unless you are linking the stylesheet with <?xml-stylesheet type="text/xsl" href="blah.xsl"?>, in which case view source shows the xml file)

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top