xml - 在 XML 中连接来自多个节点的值 - 使用 XSLT

标签 xml xslt xml-parsing

这是一个测试 XML,不是原始 XML。我只需要提取 bloggrs block 中存在 blogger ID 的那些博客站点。 是否可以仅使用 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,因为这些博客编号在博客中不存在。

最佳答案

<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

关于xml - 在 XML 中连接来自多个节点的值 - 使用 XSLT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892013/

相关文章:

xml - 如何让 XSL 在测试 XML 元素时忽略 xmlns 前缀

java - 我可以在 Java 中解析 XML,而不需要从外部获取 XML 文件输入吗?

jquery - 如何使用 jquery 过滤 xml 数据?

xml - <xsl:when test =“//…”>语法

xml - 创建 XSL 样式表以根据输入参数省略不需要的元素

c# - 使用 LINQ 从 xml 中删除元素

java - 如何在android中以编程方式包含相同的布局两次?

android - Phonegap 和 xml 文件到移动项目中的设计模式

java - 如何获取OMElement值?

java - 使用 Saxon ANT 任务从 jar 引用 XSLT