0

I am doing something wrong here. This is a sample XSL file showing the problem:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" version="4.01" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <!--<link rel="stylesheet" type="text/css" href="Workbook-off.css"/>--> <title>Custom Report</title> </head> <body> <xsl:variable name="AssignHistory" select="document('AssignHistory.xml')"/> <xsl:for-each select="$AssignHistory/AssignmentHistory/*/Items/Item[@Name='Name1']"> <xsl:apply-templates select="Item"/> </xsl:for-each> </body> </html> </xsl:template> <xsl:template match="Item"> <xsl:variable name="datestr" select="name(../..)" /> <p> <xsl:value-of select="substring($datestr, 8, 2)"/> <xsl:text>/</xsl:text> <xsl:value-of select="substring($datestr, 6, 2)"/> <xsl:text>/</xsl:text> <xsl:value-of select="substring($datestr, 2, 4)"/> <br/> <xsl:value-of select="Theme"/> <br/> <xsl:value-of select="Method"/> <br/> </p> </xsl:template> </xsl:stylesheet> 

This the the XML data:

<?xml version="1.0" encoding="UTF-8"?> <AssignmentHistory Version="1610"> <W20160104> <Items ItemCount="5"> <Item> <Name>Name1</Name> <Theme>"True Worship Requires Hard Work"</Theme> <Method>Talk</Method> </Item> <Item> <Name>Name2</Name> <Theme>Digging for Spiritual Gems</Theme> <Method>Questions and Answers</Method> </Item> <Item> <Name>Name3</Name> <Theme>Prepare This Month’s Presentations</Theme> <Method>Discussion with Video(s)</Method> </Item> <Item> <Name>Name4</Name> <Theme>"Our Privilege to Build and Maintain Places of True Worship"</Theme> <Method>Discussion with Interview(s)</Method> </Item> <Item> <Name>Name9</Name> <Theme>"Our Privilege to Build and Maintain Places of True Worship Part 2"</Theme> <Method>Discussion with Interview(s)</Method> </Item> </Items> </W20160104> <W20160111> <Items ItemCount="5"> <Item> <Name>Name5</Name> <Theme>"Jehovah Values Genuine Repentance"</Theme> <Method>Talk</Method> </Item> <Item> <Name>Name1</Name> <Theme>Digging for Spiritual Gems</Theme> <Method>Questions and Answers</Method> </Item> <Item> <Name/> <Theme>Prepare This Month’s Presentations</Theme> <Method>Discussion with Video(s)</Method> </Item> <Item> <Name>Name7</Name> <Theme>Repentance Makes a Difference</Theme> <Method>Talk</Method> </Item> <Item> <Name>Name8</Name> <Theme>Forgive Freely</Theme> <Method>Discussion with Video(s)</Method> </Item> </Items> </W20160111> </AssignmentHistory> 

Eventually I will structure it to go in a table. But at the moment I was expecting it to pull out two entries. Nothing gets displayed.

My xpath seems to be wrong or something.

3
  • Well, "nothing gets displayed" sounds as if you test the XSLT in a browser, you might want to start by installing an XSLT processor you can use from the command line or as a plugin to your favourite text editor so that you can see and post the XHTML created by the XSLT instead of simply relying on the browser. Commented May 11, 2016 at 13:22
  • @MartinHonnen I have never used a "XSLT processor". I write the XSL file in Visual Studio and execute it (in my case inside a CHtmlView browser control). Commented May 11, 2016 at 13:25
  • I think the commercial versions of Visual Studio come with an XSLT debugger so it might be a good starting point trying to use it. Commented May 11, 2016 at 13:33

2 Answers 2

2

The problem here is that:

<xsl:for-each select="$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']"> 

puts you in the context of Item. From this context:

<xsl:apply-templates select="Item"/> 

selects nothing, because Item is not a child of itself.

The simplest solution is to replace:

<xsl:for-each select="$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']"> <xsl:apply-templates select="Item"/> </xsl:for-each> 

with:

<xsl:apply-templates select="$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']"/> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That was it. That, and not using the @ character.
1

I guess $AssignHistory/AssignmentHistory/*/Items/Item[@Name='Name1'] should be $AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1'].

1 Comment

I tried removing the @ character but the results still seem to be the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.