-1

给出的 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>
4

2 回答 2

1

您尝试的主要错误是:

  • 遍历温度,然后temperature从该上下文中引用
  • 使用<代替&lt;
  • 拼错sensor-system
  • 在双引号内使用双引号
  • 使用|代替and
  • 使用绝对路径heating-is-on代替id相对temperature路径
  • (在in the之后缺少右撇号value-of
  • 结尾处缺少收盘报价value-of

以下是使用for-eachand执行此操作的方法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 &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>
于 2013-02-06T16:11:22.107 回答
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>
于 2013-02-06T17:08:45.690 回答