需要XSLT检查

时间:2013-02-06 15:49:40

标签: xml xslt

XML给出:

<?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>

2 个答案:

答案 0 :(得分:1)

您尝试的主要错误是:

  • 通过温度迭代,然后从该上下文引用temperature
  • 使用<代替&lt;
  • 拼错sensor-system
  • 在双引号内使用双引号
  • 使用|代替and
  • 使用heating-is-onidtemperature的绝对路径代替相对路径
  • (
  • 中的value-of后缺少关闭撇号
  • value-of
  • 末尾缺少收尾报价

以下是使用for-eachif

执行此操作的方法
<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 &lt; $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 &lt; $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 &lt; $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 &lt; /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>