我正在尝试利用XSLT将此天气流(截断)转换为我们的某个信息显示系统可用的内容。这是我第一次使用XSLT,所以我不知道从哪里开始。 看来我需要将每个元素名称转换为自身的属性,同时重命名元素,但我似乎无法找到这种类型转换的任何示例。如果有人知道我可以看到的任何例子,那将会有所帮助。
当前输入:
<?xml version="1.0" encoding="ISO-8859-1"?>
<current_observation version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.weather.gov/view/current_observation.xsd">
<credit>NOAA's National Weather Service</credit>
<credit_URL>http://weather.gov/</credit_URL>
<image>
<url>http://weather.gov/images/xml_logo.gif</url>
<title>NOAA's National Weather Service</title>
<link>http://weather.gov</link>
</image>
<suggested_pickup>15 minutes after the hour</suggested_pickup>
<suggested_pickup_period>60</suggested_pickup_period>
<location>Kansas City International Airport, MO</location>
<station_id>KMCI</station_id>
<latitude>39.3</latitude>
<longitude>-94.73</longitude>
</current_observation>
期望的输出:
<?xml version="1.0" encoding="ISO-8859-1"?>
<dataitems>
<dataitem name="credit">NOAA's National Weather Service</dataitem>
<dataitem name="credit_URL">http://weather.gov/</dataitem>
<dataitem name="suggested_pickup">15 minutes after the hour</dataitem>
<dataitem name="suggested_pickup_period">60</dataitem>
<dataitem name="location">Kansas City International Airport, MO</dataitem>
<dataitem name="station_id">KMCI</dataitem>
<dataitem name="latitude">39.3</dataitem>
<dataitem name="longitude">-94.73</dataitem>
</dataitems>
答案 0 :(得分:1)
此模板:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<!-- copy all nodes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- replace root element -->
<xsl:template match="current_observation">
<dataitems><xsl:apply-templates/></dataitems>
</xsl:template>
<!-- transformation for the rest of nodes -->
<xsl:template match="credit|credit_URL|suggested_pickup|suggested_pickup_period|location|station_id|latitude|longitude">
<dataitem name="{name()}"><xsl:value-of select="."/></dataitem>
</xsl:template>
<!-- delete unwanted element -->
<xsl:template match="image"/>
</xsl:stylesheet>
确实
<?xml version="1.0" encoding="ISO-8859-1"?>
<dataitems>
<dataitem name="credit">NOAA's National Weather Service</dataitem>
<dataitem name="credit_URL">http://weather.gov/</dataitem>
<dataitem name="suggested_pickup">15 minutes after the hour</dataitem>
<dataitem name="suggested_pickup_period">60</dataitem>
<dataitem name="location">Kansas City International Airport, MO</dataitem>
<dataitem name="station_id">KMCI</dataitem>
<dataitem name="latitude">39.3</dataitem>
<dataitem name="longitude">-94.73</dataitem>
</dataitems>