2012-08-08 41 views
1

这应该是简单的,一切我读到说,选择=“名称”应该在XML文档中选择所有与“名”的节点.. 因此,进出口尝试一个简单的例子..这里是XML:(它是一个简单的库存应用程序)。新建XSLT/XPath和进出口试图让过去的障碍

<?xml version="1.0"?> 
<inventory type="inventory"> 
<item type="zone" CanAdd="true"> 
    <title>Building</title> 
    <item type="porch" CanAdd="true"> 
     <title>Porch</title> 
    </item> 
    <item type="receptionRoom" CanAdd="true"> 
     <title>Reception Room</title> 
     <item type="doorInfo" CanAdd="true"> 
      <title>Door</title> 
      <item type="doorStyleType" select="multi"> 
       <title>Door Style</title> 
       <item> 
        <title>Internal</title> 
       </item> 
       <item> 
        <title>Plain</title> 
       </item> 
      </item> 
      <item type="doorWinMaterialType" select="single"> 
       <title>Door Material</title> 
       <item type="softwoodColours" select="single"> 
        <title>Softwood - Stained</title> 
        <item> 
         <title>Stained Pine</title> 
        </item> 
       </item> 
      </item> 
      <item type="doorwinFurnitureType" select="multi"> 
       <title>Door Furniture</title> 
       <item> 
        <title>Door Handle</title> 
       </item> 
       <item> 
        <title>LetterBox Opening</title> 
       </item> 
      </item> 
     </item> 
    </item> 
</item> 
<propertyName><![CDATA[2 Street, Village]]></propertyName> 
<propertyRef><![CDATA[15p9]]></propertyRef> 
</inventory> 

..我需要开始使用XSLT处理它。 (在页面后面的C#代码 - 位工作和投掷结果为弹出)

我摆弄的XSLT是:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      > 
<xsl:output method="xml" indent="yes"/> 
<xsl:template match="/"> 
    <div> 
      <h2>My Inventory</h2> 
      <table border="1"> 
       <tr> 
        <td> 
        <xsl:value-of select="inventory/propertyName" /> 
        </td> 
       </tr> 
      <xsl:for-each select ="item"> 
       <tr> 
        <td> 
         <xsl:value-of select="."/> 
        </td> 
       </tr> 
      </xsl:for-each> 
      </table> 
     <p>Hello world</p> 
    </div> 
</xsl:template> 
</xsl:stylsheet> 

的“项目”节点中未找到为每个循环,我想我已经打了一个心理障碍,为什么。我敢肯定,当你知道这个答案时,答案非常简单,但目前我不需要解决这个问题,然后我可以继续这么说。 输出是:(在浏览器中)..

My Inventory 
2 Street, Village 
(I expect a list of the "item" node values here in table rows..) 
Hello world 

非常感谢, 布雷特

回答

0

中的XPath,你已经为你的<xsl:for-each>循环指定item相对于当前节点进行评估,即相到文档根目录。为了选择在文档中的任何嵌套深度任何<item>元素,你将不得不使用

//item 

不过,我觉得你想要的东西稍有不同,因为<item>元素的文本本身是他们<title>文本以及作为任何嵌套的<item>元素的文本。也许,你正在寻找:

//item/title 

要直接对你的判决作出回应

一切我读到说,选择=“名称”应该在XML文档中选择所有与“名”的节点

这是不正确的。 select="name"选择当前节点的子节点列表中名为“name”的所有元素,而不是Xml文档中的任何位置。

+0

辉煌,谢谢你..是的,我知道我需要进一步进入树和“标题”元素。我用这个打了一个街区,而你的帮助真的很感谢。 – 2012-08-08 13:12:31

+0

..如果你知道任何好的'入门'指南链接,也将不胜感激。我已经完成了w3schools的介绍,但它们分布在不同的技术之间。再次感谢 – 2012-08-08 13:14:54

+0

@ user1584725,避免W3Schools的 - 看到为什么在这里:http://www.w3fools.com。对于链接* *好资源,请参阅:http://stackoverflow.com/questions/339930/any-good-xslt-tutorial-book-blog-site-online/341589#341589 – 2012-08-08 13:21:02