使用XML从XML文件中提取条目列表

时间:2016-05-11 13:16:23

标签: xml xpath xslt-1.0

我在这里做错了。这是一个显示问题的示例XSL文件:

<?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>

这是XML数据:

<?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>

最终我会将它构建成一张桌子。但目前我期待它能够拿出两个条目。什么都没有显示出来。

我的xpath似乎是错误的。

2 个答案:

答案 0 :(得分:2)

这里的问题是:

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

将您置于Item的上下文中。从这个背景来看:

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

什么都不选,因为Item不是自己的孩子。

最简单的解决方案是替换:

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

使用:

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

答案 1 :(得分:1)

我猜$AssignHistory/AssignmentHistory/*/Items/Item[@Name='Name1']应为$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']