Join values from multiple nodes in XML - using XSLT

六眼飞鱼酱① 提交于 2019-12-24 03:18:54

问题


This is a test XML not original XML. I am required to pull those blog sites only for which blogger ID exists in bloggrs block. Is it possible using XSLT only? I think it is not.

<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>

My output is in this form

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

This list will exclude site 6 and 7 because those bloggers number do not exist in bloggers.


回答1:


<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>




回答2:


Even xpath is enough: //blogs/url[@bloggerId = //bloggers/@bloggerId]




回答3:


This short and simple (no xsl:variable, no xsl:if) transformation:

<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>

when applied on the provided XML document:

<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>

produces the wanted, correct result:

<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>


来源:https://stackoverflow.com/questions/11892013/join-values-from-multiple-nodes-in-xml-using-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!