2014-11-04 27 views
0

大家好(你好医生尼克!)了XML的获取信息有多个与XSLT

所以我有一个小的情况下我坚持饲料。 我想用XSLT创建一个文件,它似乎不工作。

首先让我告诉你,我正在使用通过合并多重提要创建的XML文件。 其结构如下:

<sites> 
<support> 
    <rss> 
     <channel> 
      <title></title> 
      <link></link> 
      <description></description> 
      <item> 
       <link></link> 
       <title></title> 
       <description></description> 
       <guid></guid> 
       <pubDate></pubDate> 
      </item> 
     </channel> 
    </rss> 
</support> 
<corporate> 
    <rss> 
     <channel> 
      <title></title> 
      <link></link> 
      <description></description> 
      <item> 
       <link></link> 
       <title></title> 
       <description></description> 
       <guid></guid> 
       <pubDate></pubDate> 
      </item> 
     </channel> 
    </rss> 
</corporate> 

现在。我设法从一个站点接收所有信息(每个站点有多个项目),但我想从同一模板中的其他站点获取信息。

这是我做过什么:

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> 
<xsl:template match="sites"> 
<xsl:text>CHNL 1002</xsl:text> 
<xsl:apply-templates select="support/rss/channel/item"/> 
</xsl:template> 
<xsl:template match="item"> 
<xsl:variable name="ms_title" select="/sites/support/rss/channel/title"/> 
<xsl:variable name="ms_item_link" select="link"/> 
<xsl:variable name="ms_item_title" select="substring-after(title, '/&gt;')"/> 
<xsl:variable name="ms_item_date" select="pubDate"/> 
<xsl:variable name="ms_item_description" select="substring-after(description, '/&gt;&lt;br /&gt;')"/> 
<xsl:variable name="ms_item_image" select="substring-before(substring-after(description, 'src=&quot;'), '&quot;')" /> 

我可以使用变量就好了这个网站。但我想在同一个模板中使用公司网站。但我无法从那个项目中得到这些项目。 我尝试了一些东西,但最终只有第一个项目。但每个站点有多个项目。

我发现代码的变体可以从提要中获取信息。但不是来自同一模板中的合并提要。这甚至有可能实现?

我试过如下:

<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> 
<xsl:template match="sites"> 
<xsl:text>CHNL 1002</xsl:text> 
<xsl:apply-templates select="support/rss/channel/item"/> 
</xsl:template> 
<xsl:template match="item"> 
<xsl:variable name="ms_title" select="/sites/support/rss/channel/title"/> 
<xsl:variable name="ms_item_link" select="link"/> 
<xsl:variable name="ms_item_title" select="substring-after(title, '/&gt;')"/> 
<xsl:variable name="ms_item_date" select="pubDate"/> 
<xsl:variable name="ms_item_description" select="substring-after(description, '/&gt;&lt;br /&gt;')"/> 
<xsl:variable name="ms_item_image" select="substring-before(substring-after(description, 'src=&quot;'), '&quot;')" /> 
<xsl:apply-templates select="corporate/rss/channel/item"/> 
<xsl:variable name="rss_title" select="/sites/corporate/rss/channel/title"/> 
<xsl:variable name="rss_item_link" select="link"/> 
<xsl:variable name="rss_item_title" select="substring-after(title, '/&gt;')"/> 
<xsl:variable name="rss_item_date" select="pubDate"/> 
<xsl:variable name="rss_item_description" select="substring-after(description, '/&gt;&lt;br /&gt;')"/> 
<xsl:variable name="rss_item_image" select="substring-before(substring-after(description, 'src=&quot;'), '&quot;')" /> 

但后来我得到同样的信息作为第一个站点。

我希望我已经清楚地解释自己。我不是一个很好的解释者类型的人。 我希望有人能为我解答!我真的很感激! 谢谢你的时间阅读这个可怕的故事,我期待着尝试一些新的东西!

谢谢!

+0

我认为这会有所帮助,如果你能告诉我们你的预期产出。还要确保你的输入例子是正确的;目前不是。 – 2014-11-04 15:39:42

+0

我不是一个编程专家,所以我不知道我到底在做什么。这主要是谷歌搜索和反复试验。 我想要实现的是将项目的标题,描述,日期和图像获取到我正在使用的应用程序中。在左边,我想查看第一个站点和右侧的项目,我想查看第二个站点的项目。我会在ms项目变量上做一个值选择,但是我无法在rss项目变量上获得正确的值选择。 – 2014-11-04 15:51:19

回答

0

更换

<xsl:apply-templates select="support/rss/channel/item"/> 

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

然后在模板中一定要使用相对路径,以便例如

<xsl:variable name="ms_title" select="/sites/support/rss/channel/title"/> 

成为

<xsl:variable name="ms_title" select="title"/>