2

I'm trying to do a for each on each page:

<xsl:for-each select="/root/page">
   <xsl:value-of select="/root/titles/@page/"/>
</xsl:for-each>

Sample XML;

<root>
  <titles>
   <en>A title</en>
   <de>Ein Titel</de> 
 </titles>
 <page title="de">
    ....
 </page>
</root>

The problem I am having is that the XPath isn't resolving to the /root/titles/de/...
How can I get it to work?

2
  • I realize the question is quite specific, however I'm not able to come up with the right words to google it. Commented May 31, 2011 at 3:27
  • I don't think that any of the answers provided are correct. See mine if it fits. Commented May 31, 2011 at 4:17

4 Answers 4

1

updated

 <xsl:for-each select="/root/page">
   <xsl:variable name="language" select="@title"/>
   <xsl:value-of select="/root/titles/*[name()=$language]"/>
</xsl:for-each>

now it works for me!

Sign up to request clarification or add additional context in comments.

4 Comments

I cannot try at the moment but I think it's a good starting point.
I'm getting this error: Error loading stylesheet: XPath parse failure: Name or Nodetype test expected:
The result of the value-of comes back blank for me but the path prints out. EDIT: Nevermind... its a completely unrelated issue
ah ok! I was investigating further! np ;-)
1

Use xsl:key. Because is not clear the output you need I've provided just an example for you showing how to get the title from titles for each page according to the page language.


XSLT 1.0 tested under Saxon 6.5.5

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="titles" match="titles/*" use="local-name()"/>

    <xsl:template match="/root">
     <xsl:for-each select="page">
             <xsl:value-of select="concat('title-',@title,key('titles',@title),'&#xA;')"/>
     </xsl:for-each>
 </xsl:template>

</xsl:stylesheet>

Applied on this input

 <?xml version="1.0" encoding="UTF-8"?>
<root>
  <titles>
   <en>A title</en>
   <de>Ein Titel</de> 
 </titles>
 <page title="de">
    ....
    </page>
     <page title="en">
    ....
 </page>
</root>

produces:

title-de:Ein Titel
title-en:A title

Tested on Mozilla Firefox 3.6.17 as follows:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test_trans.xsl"?>
<root>
  <titles>
   <en>A title</en>
   <de>Ein Titel</de> 
 </titles>
 <page title="de">
    ....
    </page>
     <page title="en">
    ....
 </page>
</root>

Produces

 title-deEin Titeltitle-enA title

Obviously without new lines, because we are displaying it in a browser and the transform should be changed to produce HTML.

2 Comments

What about for Firefox? I'm getting a "Error loading stylesheet: Parsing an XSLT stylesheet failed. " only on the key.
Tried on Mozilla Firefox 3.6.17 and works fine as expected. See my answer edit.
1

It's not really clear what you want, but it looks like you should be aware that xsl:for-each changes the context to the selected node, just like a template does. So this might get you somewhere:

<xsl:for-each select="/root/page">
   <xsl:value-of select="/root/titles/*[name()=current()/@title]"/>
</xsl:for-each>

Hope this helps.

2 Comments

I'm wanting the XSL to grab /root/title/de because de was an attribute within <page>..</page>
@monksy, sorry, misread, updated my answer. Basically like danyolgiax's only without the extra variable. You can also use local-name, but it's only needed if you're dealing with namespaces.
0
<xsl:for-each select="/root/page">
    <xsl:variable name="language" select="@title"\>
    <xsl:value-of select="/root/titles/$language"/>
</xsl:for-each>

1 Comment

I updated the code... I just thought I'd give you fair warning

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.