2012-08-09 56 views
2

這是一個測試XML而非原始XML。我需要拉博客站點中存在博客ID的博客網站。 只能使用XSLT嗎?我認爲這不是。使用XSLT從多個節點中加入值 - 使用XSLT

<root> 
<bloggers> 
<name bloggerId = "1">Jacob Sebastian</name> 
<name bloggerId = "2">Adam Machanic</name> 
<name bloggerId = "3">Pinal Dave</name> 
<name bloggerId = "4">Steve Jones</name> 
<name bloggerId = "5">Michael Coles</name> 
</bloggers> 
<blogs> 
<url bloggerId = "1">http://www.sqlblog.com/adam_machanic </url> 
<url bloggerId = "2">http://www.sqlauthority.com </url> 
<url bloggerId = "3">http://www.beyondrelational.com </url> 
<url bloggerId = "4">http://www.sqlblog.com/michael_coles </url> 
<url bloggerId = "5">http://www.sqlservercentral.com/blogs/steve_jones </url> 
<url bloggerId = "6">http://www.cnn.com/belief </url> 
<url bloggerId = "7">http://www.yahoo.com/360 </url> 
</blogs> 
</root> 

我的輸出以這種形式

+-------+----------+ 
| Site | Blogger | 
+-------+----------+ 
| site1 | blogger1 | 
| site2 | blogger2 | 
+-------+----------+ 

此列表將排除網站6和7,因爲這些博客數量不存在的博客。

回答

2
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <table border="1"> 
      <xsl:apply-templates select="//url" /> 
     </table> 
    </xsl:template> 

    <xsl:template match="url"> 
     <xsl:variable name="id" select="@bloggerId" /> 

     <xsl:if test="count(//bloggers//name[@bloggerId = $id]) &gt; 0"> 
      <tr> 
       <td> 
        <xsl:value-of select="//bloggers//name[@bloggerId = $id]" /> 
       </td> 
       <td> 
        <xsl:value-of select="." /> 
       </td> 
      </tr> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

enter image description here

+0

感謝大家的回答這個問題。我相信所有的答案都是正確的。在問之前我確定這是不可能的,但我仍然問:)很高興我問。 – 2012-08-10 20:56:14

2

即使XPath是不夠的://blogs/url[@bloggerId = //bloggers/@bloggerId]

1

這個簡短和簡單(無xsl:variable,沒有xsl:if)變換

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/*"> 
    <table border="1"> 
    <thead> 
    <th>Site</th><th>Blogger</th> 
    </thead> 
    <tbody> 
    <xsl:apply-templates/> 
    </tbody> 
    </table> 
</xsl:template> 

<xsl:template match= 
    "url[@bloggerId = /*/bloggers/name/@bloggerId]"> 
    <tr> 
    <td><a href="{.}"><xsl:value-of select="."/></a></td> 
    <td><xsl:value-of select= 
    "/*/bloggers/name[@bloggerId = current()/@bloggerId]"/></td> 
    </tr> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

當所提供的應用XML文檔

<root> 
    <bloggers> 
     <name bloggerId = "1">Jacob Sebastian</name> 
     <name bloggerId = "2">Adam Machanic</name> 
     <name bloggerId = "3">Pinal Dave</name> 
     <name bloggerId = "4">Steve Jones</name> 
     <name bloggerId = "5">Michael Coles</name> 
    </bloggers> 
    <blogs> 
     <url bloggerId = "1">http://www.sqlblog.com/adam_machanic</url> 
     <url bloggerId = "2">http://www.sqlauthority.com</url> 
     <url bloggerId = "3">http://www.beyondrelational.com</url> 
     <url bloggerId = "4">http://www.sqlblog.com/michael_coles</url> 
     <url bloggerId = "5">http://www.sqlservercentral.com/blogs/steve_jones</url> 
     <url bloggerId = "6">http://www.cnn.com/belief</url> 
     <url bloggerId = "7">http://www.yahoo.com/360</url> 
    </blogs> 
</root> 

產生想要的,正確的結果

<table border="1"> 
    <thead> 
     <th>Site</th> 
     <th>Blogger</th> 
    </thead> 
    <tbody> 
     <tr> 
     <td> 
      <a href="http://www.sqlblog.com/adam_machanic">http://www.sqlblog.com/adam_machanic</a> 
     </td> 
     <td>Jacob Sebastian</td> 
     </tr> 
     <tr> 
     <td> 
      <a href="http://www.sqlauthority.com">http://www.sqlauthority.com</a> 
     </td> 
     <td>Adam Machanic</td> 
     </tr> 
     <tr> 
     <td> 
      <a href="http://www.beyondrelational.com">http://www.beyondrelational.com</a> 
     </td> 
     <td>Pinal Dave</td> 
     </tr> 
     <tr> 
     <td> 
      <a href="http://www.sqlblog.com/michael_coles">http://www.sqlblog.com/michael_coles</a> 
     </td> 
     <td>Steve Jones</td> 
     </tr> 
     <tr> 
     <td> 
      <a href="http://www.sqlservercentral.com/blogs/steve_jones">http://www.sqlservercentral.com/blogs/steve_jones</a> 
     </td> 
     <td>Michael Coles</td> 
     </tr> 
    </tbody> 
</table>