Avatar billede marvas Nybegynder
27. oktober 2005 - 09:28 Der er 6 kommentarer

using "unparsed-text" function with variable values

HI There

i am converting an xml file to another xml file with a different format.  in my older xml file i have a tag which contains a file name.  this file is a txt file, whose contents i would like to show as the contents of an attribute in my new xml. 

here's a quick example:
Old xml: <FILE>title.txt</FILE>
New xml: <TAG attr1="sometext" attr2="content of title.txt"/>

i am using xsl:stylesheet version="2.0" , and trying to work with the function unparsed-text as follows:
<xsl:attribute name="attr2" select="fn:unparsed-text('title.txt', 'utf-8')"/>
the above statement works fine, but as soon as i try using a variable instead of an absolute value for the file name, i get "Error in XPath 2.0 expression". here's my code:
<xsl:variable name="v1" select="FILE"/>
<xsl:variable name="v1" select="unparsed-text('$v1', 'utf-8')"/>

any idea why this is not working, and how i might solve this problem?
Avatar billede janegil Nybegynder
27. oktober 2005 - 11:08 #1
"title.txt" is a relative URL.

But is it relative to some http:URL, or to a OS file name? Mye guess is that this is more likely to work with HTTP: than with file:.
Avatar billede Slettet bruger
27. oktober 2005 - 11:26 #2
If it's not a typo (or redefinition is allowed in 2.0), the reason it's failing is because you cannot redefine a variable. Give the second variable another name (say, 'v2') AND you should probably try a slight variation of quoting when using variables/parameters:

<xsl:variable name="v1" select="'title.txt'"/>
<xsl:variable name="v2" select="unparsed-text($v1, 'utf-8')"/>

If variables are allowed (I have no idea) as arguments, then the above should definitely work, if this one (as you say) works:

  <xsl:attribute name="attr2" select="fn:unparsed-text('title.txt', 'utf-8')"/>

/CS
Avatar billede Slettet bruger
27. oktober 2005 - 11:49 #3
Oops - forgot to include the namespace-prefix (just copied from your example - could be another reason for it not to work):

<xsl:variable name="v1" select="'title.txt'"/>
<xsl:variable name="v2" select="fn:unparsed-text($v1, 'utf-8')"/>

/CS
Avatar billede marvas Nybegynder
27. oktober 2005 - 12:36 #4
my bad.  i need to include the contents of the text file in an attribute. so after having assigned the contents of the file to a variable:
<xsl:variable name="v1" select="FILE"/>, where file is the tag in the original xml that contains the path to the txt file
then i want to assign the contents to an attribute in the second xml file:
<xsl:attribute name="attr2" select="unparsed-text($v1)"/>

that's what's not working.  whenever i try to insert the path itself: unparsed-text('title.txt'), it works fine.
Avatar billede Slettet bruger
27. oktober 2005 - 14:02 #5
Ah, OK...

If <FILE> is in another file, use the document() function, something like this:

<xsl:attribute name="attr2" select="unparsed-text(document('files.xml')/FILES/FILE)"/>

/CS
Avatar billede Slettet bruger
27. oktober 2005 - 14:07 #6
I'm not reading what you're writing :-) "FILE is in the *original* xml" ...

Well, then the problem is most likely that the variable holds the element and not the value of it - try this when assigning to v1:

  <xsl:variable name="v1" select="string(FILE)"/>

or even:

  <xsl:variable name="v1"><xsl:value-of select="FILE" /></xsl:variable>

/CS
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester