我有一个XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<metadata>
<field name="MyWebsite/Metadata/Keywords">key1</field>
<field name="MyWebsite/Metadata/active_url">link111</field>
</metadata>
<metadata>
<field name="MyWebsite/Metadata/Keywords">key2</field>
<field name="MyWebsite/Metadata/active_url">link222</field>
</metadata>
<metadata>
<field name="MyWebsite/Metadata/Keywords">key3</field>
<field name="MyWebsite/Metadata/active_url">link333</field>
</metadata>
</document>
使用XSL,结果应为 -
Name: MyWebsite/Metadata/Keywords key1
Name: MyWebsite/Metadata/active_url link111
Name: MyWebsite/Metadata/Keywords key2
Name: MyWebsite/Metadata/active_url link222
Name: MyWebsite/Metadata/Keywords key3
Name: MyWebsite/Metadata/active_url link333
我尝试了这个,但它没有在第二列上工作 -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Updated Tool Information:</h2>
<table border="1">
<xsl:for-each select="document/metadata/field">
<tr>
<td>Name: <xsl:value-of select="@name" /></td>
<td><xsl:value-of select="field/@name=MyWebsite/Metadata/Keywords" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
有什么想法怎么做?
答案 0 :(得分:1)
问题在于这一行
<xsl:value-of select="field/@name=MyWebsite/Metadata/Keywords" />
从查看您的预期输出看起来您只想输出当前字段元素的值,所以也许您应该这样做
<xsl:value-of select="." />
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Updated Tool Information:</h2>
<table border="1">
<xsl:for-each select="document/metadata/field">
<tr>
<td>Name: <xsl:value-of select="@name" /></td>
<td><xsl:value-of select="." /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>