【问题标题】:find a right xslt for given data为给定数据找到正确的 xslt
【发布时间】:2023-01-12 21:18:38
【问题描述】:

数据 xml 形式并转换和过滤一些信息... 使用轴和以下兄弟...

    -----------------------------Source XML------------------------   <FY2019Temperature>
       <January>
    <High>-1</High>
    <Low>-9</Low>
     </January>
       <February>
    <High>-1</High>
    <Low>-9</Low>
</February>
<March>
    <High>21</High>
    <Low>9</Low>
</March>
<April>
    <High>21</High>
    <Low>9</Low>
</April>
<May>
    <High>21</High>
    <Low>9</Low>
</May>
<June>
    <High>21</High>
    <Low>9</Low>
</June>
<July>
    <High>29</High>
    <Low>18</Low>
</July>
<August>
    <High>29</High>
    <Low>18</Low>
</August>
<September>
    <High>24</High>
    <Low>12</Low>
</September>
<October>
    <High>24</High>
    <Low>12</Low>
</October>
<November>
    <High>12</High>
    <Low>-1</Low>
</November>
<December>
    <High>-1</High>
    <Low>-9</Low>
</December>

- - - - - - - - -预期结果 - - - - - - - - - - - - - -

Months Range,Temperature Range
January to February,-1 to -9
March to June,21 to 9
July to August,29 to 18
September to October,24 to 12
November,12 to -1
December,-1 to -9

【问题讨论】:

  • 在 XSLT 3 中就是 &lt;xsl:for-each-group select="*" composite="yes" group-adjacent="High, Low"&gt;{name()}{(' to ' || name(current-group()[last()]))[current-group()[2]]}, {High} to {Low}&amp;#10;&lt;/xsl:for-each-group&gt;
  • 请给我一个完整的 XSLT...

标签: xslt-1.0 xslt-2.0


【解决方案1】:

尝试这个。它是为 1.0 编写的,因为问题有一个 1.0 标签。

<xsl:template match="/*">
  <xsl:variable name="months">
    <xsl:for-each select="*">
      <xsl:variable name="High" select="High"/>
      <xsl:variable name="Low" select="Low"/>
    
      <xsl:element name="element">
        <xsl:element name="month">
          <xsl:value-of select="local-name()"/>
        </xsl:element>
        <xsl:copy-of select="High"/>
        <xsl:copy-of select="Low"/>
        <xsl:element name="first">
          <xsl:if test="not(preceding-sibling::*[1][High = $High and Low = $Low])">
            <xsl:value-of select="'true'"/>
          </xsl:if>
        </xsl:element>
        <xsl:element name="last">
          <xsl:if test="not(following-sibling::*[1][High = $High and Low = $Low])">
            <xsl:value-of select="'true'"/>
          </xsl:if>
        </xsl:element>
      </xsl:element>
    </xsl:for-each>
  </xsl:variable>

  <!-- Be sure to use your own namespace prefix for node-set. -->
  <xsl:variable name="monthList" select="msxml:node-set($months)"/>

  <xsl:value-of select="concat('Months Range,Temperature Range', '&#13;')"/>

  <xsl:for-each select="$monthList/element">
    <xsl:choose>
      <xsl:when test="first = 'true' and last = 'true'">
        <xsl:value-of select="concat(month, ',', High, ' to ', Low, '&#13;')"/>
      </xsl:when>
      <xsl:when test="first = 'true'">
        <xsl:value-of select="month"/>
      </xsl:when>
      <xsl:when test="last = 'true'">
        <xsl:value-of select="concat(' to ', month, ',', High, ' to ', Low, '&#13;')"/>
      </xsl:when>
    </xsl:choose>
  </xsl:for-each>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多