从键值对动态填充列 [英] Populating column dynamically from key value pair

查看:19
本文介绍了从键值对动态填充列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 xslt 文件进行样式设置.我正在以表格格式显示一些内容.我需要从预定义的键值对中动态填充一列.请看下面的例子

I am working with an xslt file for styling. I am showing some content in tabular format. I need to dynamically populate one column from pre defined key value pair. Please look at below example

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
  <h2>Sample Transformation</h2>
    <table border="1">
    <xyz>msgprop</xyz>
      <tr bgcolor="#9acd32">
        <th>Id</th>
        <th>Name</th>
        <th>City</th>
      </tr>
      <xsl:for-each select="en:MyEvent">
      <tr>
        <td><xsl:value-of select="en:id"/></td>
        <td><xsl:value-of select="en:name"/></td>
        <td><xsl:value-of select="en:country"/></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

问题是我正在接收国家名称而不是城市,我想用正确的城市填充城市列,为此我需要映射国家及其城市.有没有办法在键值对中预定义国家及其城市,当收到国家名称时,它将被城市名称替换(如果不存在,它将显示国家名称).有什么功能可以做到吗?如果你提供一个片段会很有帮助.

The problem is that i am receiving country name in place of city and i want to populate the city column with correct city and for that i need to map country and its city. Is there any way to predefined country and its city in key value pair and when received the country name, it will be replaced by city name(if not present it will display country name). Is there any function to do it?. It will be helpfull if you provide a snippet.

这是xml

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

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

I need a mapping such that whenever country name is UK it will be replaced with its capital London same with other countries. Is there somew way that i define UK as a key and London as a value so that whenever i received key as UK it will be replaced with its value. Please help.

推荐答案

考虑以下示例:

XML

<events>
    <event>
        <id>001</id>
        <name>Alpha</name>
        <city>Paris</city>
    </event>
    <event>
        <id>002</id>
        <name>Bravo</name>
        <city>UK</city>
    </event>
    <event>
        <id>003</id>
        <name>Charlie</name>
        <city>Berlin</city>
    </event>
    <event>
        <id>004</id>
        <name>Delta</name>
        <city>USA</city>
    </event>
    <event>
        <id>005</id>
        <name>Echo</name>
        <city>Los Angeles</city>
    </event>
</events>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/events">
    <html>
        <body>
            <h2>Sample Transformation</h2>
            <table border="1">
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>City</th>
                </tr>
                <xsl:for-each select="event">
                    <tr>
                        <td>
                            <xsl:value-of select="id"/>
                        </td>
                        <td>
                            <xsl:value-of select="name"/>
                        </td>
                        <td>
                            <xsl:choose>
                                <xsl:when test="city = 'UK'">London</xsl:when>
                                <xsl:when test="city = 'USA'">Washington, D.C.</xsl:when>
                                <!-- add more key/value pairs here -->
                                <xsl:otherwise>
                                    <xsl:value-of select="city"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

结果(渲染):

这篇关于从键值对动态填充列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆