在 XML 中连接来自多个节点的值 - 使用 XSLT [英] Join values from multiple nodes in XML - using XSLT

查看:18
本文介绍了在 XML 中连接来自多个节点的值 - 使用 XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个测试 XML,而不是原始 XML.我只需要为 bloggrs 块中存在博主 ID 的那些博客站点拉取.是否可以只使用 XSLT?我认为不能.

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>

我的输出是这种形式

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

此列表将排除站点 6 和 7,因为博主中不存在这些博主编号.

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

推荐答案

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

这篇关于在 XML 中连接来自多个节点的值 - 使用 XSLT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆