2012-12-10 137 views
0

我是使用XSL和XML的初学者。我在从XML文档中获取数据并在XSL中使用它时遇到了问题。目前我正在尝试不同的方法,但似乎无法做到正确,我将需要使用此技术来完成项目。一旦我可以做到这一点,我将能够在整个页面上做到。将xml文档中的数据插入到xsl中

这里是我的XML样本和我的XSL文件:

<storelocator> 
    <!--Parent element containing all text on the page--> 
    <content_text> 
     <title> 
      <titlename>Store Locator</titlename> 
     </title> 
     <title> 
      <subtitle>FIND A STORE</subtitle> 
     </title> 
     <title> 
      <countrydropdown>Country</countrydropdown> 
     </title> 
     <title> 
      <address>Steet Address, City, Sate/Province OR Postal/Zip Code</address> 
     </title> 
     <title> 
      <radiusdropdown>Radius</radiusdropdown> 
     </title> 
     <title> 
      <featuredstore>FEATURED STORE</featuredstore> 
     </title> 
     <title> 
      <storelocation>Newbury Street, Boston, MA USA</storelocation> 
     </title> 
</storelocator> 

这里是XSL:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/"> 
<html> 
<body bgcolor="white"> 

<div id="container" style="100%"> 

<div id="header" style="background: url('header.png'); height:30px;"></div> 

<div id="content_container" align="center" style="text-transform: uppercase; font-family: tahoma; font-weight: normal; font-size: 0.8em"> 
    <div id="content" align="left" style="background-color:blue;height:845px;width:848px"> 

    <xsl:value-of select="titlename"/> <!--here I was trying to import the text from XML DOC--> 

<br/> 
<br/> 
<br/> 
    <img src="image.png" align="left"/><br/> 
<br/> 
<br/> 
<br/> 
<br/> 

</div> 
</div> 

<div id="footer" style="background-color:black;clear:both;text-align:center;height:20px"></div> 
</div> 
</body> 
</html> 

</xsl:template> 
</xsl:stylesheet> 

基本上什么,我试图做的是进口的“商店定位器”称号等等它显示在图像上方,但在浏览器中打开时目前没有任何显示。我非常感谢一些帮助,因为我一直在试图做一些。

回答

2

XSLT中要记住的最重要的事情之一就是所有路径都将基于上下文。在你的例子中,上下文是/,因为在你的模板中,你匹配/。你不能直接从/titlename

试着改变你的xsl:value-of到:

<xsl:value-of select="storelocator/content_text/title/titlename"/> 

此外,XSLT没有 “进口” 你的XML;它正在改变它。如果你只是在浏览器中打开XSLT,这是行不通的。为了得到转换打开XML时,在浏览器中工作,你必须添加指向您的XSLT处理指令:

<?xml-stylesheet type="text/xsl" href="test.xsl"?> 
<storelocator> 
... 
</storelocator> 
+0

谢谢这个工作X – Ibrahim

+0

@Ibrahim - 非常欢迎。 +1是一个很好的问题。 –