<?xml version="1.0" encoding="UTF-8"?>
<sensor-system>
<velocity>120.00</velocity> <!-- km/h -->
<temperature location="inside">24.6</temperature>
<temperature location="outside">-12.5</temperature>
<seats>
<seat location="front">
<id>1</id>
<temperature>32.5</temperature>
<heating-is-on/>
</seat>
<seat location="back">
<id>2</id>
<temperature>23.5</temperature>
</seat>
</seats>
</sensor-system>
d)XSLT(25分) 编写一个XSL转换,接收给定的XML作为输入,并输出带有所有座位ID号的文本,其中相应座位的温度低于内部温度,并且加热被切掉。 给定XML的输出应如下所示: 关闭加热的冷座椅:2(23.5) 第一个数字是座位id,第二个是座位温度。
我的回答请检查:
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="/sensor-system/seats/seat/temperature">
<xsl:if test="(temperature < /sesnsor-system/temperature[@location="inside"]) | not(/sensor-system/seats/seat/heating-is-on)" >
<xsl:value-of select="concat('cold seats with heating switched of : ' , '',/sensor-system/seats/seat/id,'(,/sensor-system/seats/seat/temperature,')')/>
</xsl:if>
</xsl:for-each>
</xsl:template>
答案 0 :(得分:1)
您尝试的主要错误是:
temperature
<
代替<
sensor-system
|
代替and
heating-is-on
,id
和temperature
的绝对路径代替相对路径(
value-of
后缺少关闭撇号
value-of
以下是使用for-each
和if
:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="insideTemp"
select="/sensor-system/temperature[@location='inside']" />
<xsl:text>cold seats with heating switched off: </xsl:text>
<xsl:for-each select="/sensor-system/seats/seat">
<xsl:if test="temperature < $insideTemp and not(heating-is-on)" >
<xsl:value-of select="concat(id, ' (', temperature, ') ')" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
使用for-each
中的谓词:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="insideTemp"
select="/sensor-system/temperature[@location='inside']" />
<xsl:text>cold seats with heating switched off: </xsl:text>
<xsl:for-each select="/sensor-system/seats/seat[temperature < $insideTemp and
not(heating-is-on)]">
<xsl:value-of select="concat(id, ' (', temperature, ') ')" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
使用模板:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="insideTemp"
select="/sensor-system/temperature[@location='inside']" />
<xsl:text>cold seats with heating switched off: </xsl:text>
<xsl:apply-templates select="/sensor-system/seats/seat
[temperature < $insideTemp and not(heating-is-on)]" />
</xsl:template>
<xsl:template match="seat">
<xsl:value-of select="concat(id, ' (', temperature, ') ')" />
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="/sensor-system/seats/seat">
<xsl:if test="temperature < /sensor-system/temperature[@location='inside'] or not(heating-is-on) " >
<xsl:text>cold seats with heating switched of : </xsl:text>
<xsl:value-of select="id"/> <xsl:text> </xsl:text>
<xsl:value-of select="temperature"/>
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>