2011-08-05 93 views
-1

我在这里遇到了这个非常奇怪的问题。我无法获得以下xml文件的节点值。任何人都可以告诉我我的xslt文件或xml文件有什么问题吗?因为我可以让xslt在其他xml文件上工作,而且非常简单。所以我退出了这里。 xml文件无法在XSLT中获取节点值

<catalog xmlns="http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0" xmlns:xlink="http://www.w3.org/1999/xlink" name="Test Data"> 
    <service name="tss" serviceType="OpenDAP" base="http://virbo.org/metamag/viewDataFile.jsp?filetype=data&amp;docname="/> 
    <dataset name="Scalar"> 
     <access serviceName="tss" urlPath="597C7956-742D-FEC6-D151-A37A7176E867"/> 
     <documentation type="summary">Single variable time series</documentation> 
    </dataset> 
    <dataset name="Structure"> 
     <access serviceName="tss" urlPath="E981F1AF-EF4A-11FB-AFB6-F20218B07783"/> 
     <documentation type="summary">Vector (three component) time series</documentation> 
    </dataset> 
    <dataset name="Sequence"> 
     <access serviceName="tss" urlPath="64C78182-9BDC-CBC4-56C5-679808F51398"/> 
     <documentation type="summary">Spectrum time series</documentation> 
    </dataset> 
</catalog> 

XSLT文件

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html"/> 
    <xsl:template match="/"> 
     <h2>database</h2> 
    <li>  
      <xsl:for-each select="/catalog/dataset/access/@serviceName"> 
       <xsl:value-of select="."/> 
      </xsl:for-each> 
    </li> 
    </xsl:template> 
</xsl:stylesheet> 

但XSLT文件可在以下XML文件只是改变了XPath来/数据/学生/名/ @ ID

<data class="grade2"> 
    <student id="test1"> 
     <name id="1">Bitu Kumar</name> 
     <course>MCA</course> 
     <sem>6</sem> 
     <marks>80</marks> 
    </student> 
    <student id="test2"> 
     <name id="2">Santosh Kumar</name> 
     <course>MCA</course> 
     <sem>5</sem> 
     <marks>70</marks> 
    </student> 
    <student id="test3"> 
     <name id="3">Ashish</name> 
     <course>M.Sc.</course> 
     <sem>4</sem> 
     <marks>80</marks> 
    </student> 
    <student id="test4"> 
     <name id="4">Mahesh</name> 
     <course>MA</course> 
     <sem>3</sem> 
     <marks>80</marks> 
    </student> 
</data> 

回答

0

您的XSLT不尊重第一个XML文件中的名称空间。

你的第二个XML文件没有任何。

通过添加一个名称空间声明来修复您的XSLT,并更改您的XPath表达式,以便它使用您定义的任何名称空间前缀。

这样:

xmlns:myprefix="http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0" 

...

/myprefix:catalog/myprefix:dataset/myprefix:access/@serviceName 
+0

非常感谢您的答复,您可以举一个例子如何使用命名空间前缀的XPath? – user851380

+0

我应该添加什么名字空间?这个? – user851380

+0

xmlns =“http://www.unidata.ucar.edu/namespaces/thredds/InvCatalog/v1.0” – user851380