我有这个XML
<?xml version="1.0" encoding="UTF-8"?>
<document>
<metadata>
<field name="MyWebsite/Metadata/Keywords">marketing business</field>
<field name="MyWebsite/Metadata/PageTitle">PageTitle1</field>
<field name="MyWebsite/Metadata/active_url">marketing business link</field>
</metadata>
<metadata>
<field name="MyWebsite/Metadata/Keywords">planning development</field>
<field name="MyWebsite/Metadata/PageTitle">PageTitle2</field>
<field name="MyWebsite/Metadata/active_url">planning development link</field>
</metadata>
<metadata>
<field name="MyWebsite/Metadata/Keywords">learning development</field>
<field name="MyWebsite/Metadata/PageTitle">PageTitle3</field>
<field name="MyWebsite/Metadata/active_url">learning development link</field>
</metadata>
我的XSL代码是
<?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>
<xsl:if test = "@name = 'MyWebsite/Metadata/Keywords' and contains(.,'development')">
<td>Page Title: <xsl:value-of select="field[@name = 'MyWebsiteMetadata/PageTitle']"/></td>
<td>URL: <xsl:value-of select="field[@name = 'MyWebsiteMetadata/active_url']"/> </td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
我的目标是将字符串与'development'匹配,该页面将仅显示Page title和URL。
我的预期结果应该显示如下 -
不幸的是,上面的代码没有显示任何内容,它是空白的。
答案 0 :(得分:0)
而不是
<xsl:for-each select="document/metadata/field">
<tr>
<xsl:if test = "@name = 'MyWebsite/Metadata/Keywords' and contains(.,'development')">
<td>Page Title: <xsl:value-of select="field[@name = 'MyWebsiteMetadata/PageTitle']"/></td>
<td>URL: <xsl:value-of select="field[@name = 'MyWebsiteMetadata/active_url']"/> </td>
</xsl:if>
</tr>
</xsl:for-each>
你想要
<xsl:for-each select="document/metadata[field[@name = 'MyWebsite/Metadata/Keywords' and contains(.,'development')]]">
<tr>
<td>Page Title: <xsl:value-of select="field[@name = 'MyWebsiteMetadata/PageTitle']"/></td>
<td>URL: <xsl:value-of select="field[@name = 'MyWebsiteMetadata/active_url']"/> </td>
</tr>
</xsl:for-each>