使用XSLT在XML中连接来自多个节点的值
这是一个测试XML,不是原始XML。我需要拉这些博客网站只有博客ID存在于bloggrs块是否可以只使用XSLT?我认为不可能使用XSLT在XML中连接来自多个节点的值,xml,xslt,xml-parsing,Xml,Xslt,Xml Parsing,这是一个测试XML,不是原始XML。我需要拉这些博客网站只有博客ID存在于bloggrs块是否可以只使用XSLT?我认为不可能 <root> <bloggers> <name bloggerId = "1">Jacob Sebastian</name> <name bloggerId = "2">Adam Machanic</name> <name bloggerId = "3">Pinal Dave</n
<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>
此列表将排除站点6和7,因为这些bloggers编号在bloggers中不存在。甚至xpath也足够了:
//blogs/url[@bloggerId=//bloggers/@bloggerId]
<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]) > 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>
这个简短而简单的(无
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>
<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>
网站博主
应用于提供的XML文档时:
<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>
<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>
雅各布·塞巴斯蒂安
亚当机械
皮纳尔·戴夫
史蒂夫·琼斯
迈克尔·科尔斯
http://www.sqlblog.com/adam_machanic
http://www.sqlauthority.com
http://www.beyondrelational.com
http://www.sqlblog.com/michael_coles
http://www.sqlservercentral.com/blogs/steve_jones
http://www.cnn.com/belief
http://www.yahoo.com/360
生成所需的正确结果:
<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>
<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>
场地
博客作者
雅各布·塞巴斯蒂安
亚当机械
皮纳尔·戴夫
史蒂夫·琼斯
迈克尔·科尔斯
感谢大家回答这个问题。我相信所有答案都是正确的。在问之前,我确信这是不可能的,但我还是问了:)很高兴我问了。