首页 > 解决方案 > 替换 xsl/xslt 中的值

问题描述

我正在研究一个 xsl 样式表。我以表格格式显示标题、艺术家和他们的国家。现在我需要用他们的首都替换国家。如何创建映射(键,值)并将其替换为列。下面是我的示例 xml。

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
  </cd>
  <cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
  </cd>
  <cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
  </cd>
</catalog>

这是我正在使用的样式表

<?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>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Title</th>
      <th style="text-align:left">Artist</th>
<th style="text-align:left">Country</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="country"/></td>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

我需要一个映射,这样每当国家名称为英国时,它将被替换为与其他国家相同的首都伦敦。是否有某种方法可以将 UK 定义为键,将 London 定义为值,这样每当我收到作为 UK 的键时,它将被其值替换。请帮忙。

标签: xmlxsltxslt-1.0

解决方案


推荐阅读