Andrew Truckle
Andrew Truckle

Reputation: 19157

Extracting a list of entries from a XML file with XML

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.

Upvotes: 0

Views: 38

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 117083

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']"/>

Upvotes: 2

Martin Honnen
Martin Honnen

Reputation: 167696

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

Upvotes: 1

Related Questions