XML Tutorial/XSLT stylesheet/number function

Материал из Web эксперт
Версия от 08:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

conversion of boolean values true and false

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <text>124</text>
    <text>1 2 4</text>
    <text>-16</text>
    <text>- 16</text>
    <text>125.258</text>
    <text>125.</text>
    <text>ASDF</text>
    <text>A123</text>
    <text>true</text>
    <text>false()</text>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE border="1">
        <TR>
          <td>text</TH>
          <td>number</TH>
        </TR>
        <xsl:for-each select="//text[text() = "true" or text() = "false()"]">
          <TR>
            <TD>
              <xsl:value-of select="."/>
            </TD>
            <TD>
              <xsl:value-of select="number()"/>
            </TD>
          </TR>
        </xsl:for-each>
      </TABLE>
      <Paragraph>
        <xsl:text>but:</xsl:text>
      </Paragraph>
      <TABLE border="1">
        <TR>
          <td>function</TH>
          <td>number</TH>
        </TR>
        <TR>
          <TD>true()</TD>
          <TD>
            <xsl:value-of select="number(true())"/>
          </TD>
        </TR>
        <TR>
          <TD>false()</TD>
          <TD>
            <xsl:value-of select="number(false())"/>
          </TD>
        </TR>
        <TR>
          <TD>5>7</TD>
          <TD>
            <xsl:value-of select="number(5 > 7)"/>
          </TD>
        </TR>
        <TR>
          <TD>5&lt;7</TD>
          <TD>
            <xsl:value-of select="number(5&lt;7)"/>
          </TD>
        </TR>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE border="1"><TR><td>text</TH><td>number</TH></TR><TR><TD>true</TD><TD>NaN</TD></TR><TR><TD>false()</TD><TD>NaN</TD></TR></TABLE><Paragraph>but:</Paragraph><TABLE border="1"><TR><td>function</TH><td>number</TH></TR><TR><TD>true()</TD><TD>1</TD></TR><TR><TD>false()</TD><TD>0</TD></TR><TR><TD>5&gt;7</TD><TD>0</TD></TR><TR><TD>5&lt;7</TD><TD>1</TD></TR></TABLE>


demonstrates string conversion

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <text>124</text>
    <text>1 2 4</text>
    <text>-16</text>
    <text>- 16</text>
    <text>125.258</text>
    <text>125.</text>
    <text>ASDF</text>
    <text>A123</text>
    <text>true</text>
    <text>false()</text>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <TABLE border="1">
        <TR>
          <td>text</TH>
          <td>number</TH>
        </TR>
        <xsl:for-each select="//text">
          <TR>
            <TD>
              <xsl:value-of select="."/>
            </TD>
            <TD>
              <xsl:value-of select="number()"/>
            </TD>
          </TR>
        </xsl:for-each>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><TABLE border="1"><TR><td>text</TH><td>number</TH></TR><TR><TD>124</TD><TD>124</TD></TR><TR><TD>1 2 4</TD><TD>NaN</TD></TR><TR><TD>-16</TD><TD>-16</TD></TR><TR><TD>- 16</TD><TD>NaN</TD></TR><TR><TD>125.258</TD><TD>125.258</TD></TR><TR><TD>125.</TD><TD>125</TD></TR><TR><TD>ASDF</TD><TD>NaN</TD></TR><TR><TD>A123</TD><TD>NaN</TD></TR><TR><TD>true</TD><TD>NaN</TD></TR><TR><TD>false()</TD><TD>NaN</TD></TR></TABLE>


if element value is a number

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <number>253</number>
    <number>sd12</number>
    <number>-64</number>
    <number>2.25.25</number>
    <number>2.25</number>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="number">
      <DIV>
        <xsl:value-of select="."/>
        <xsl:if test="string(number(.))="NaN""> is not a number</xsl:if>
      </DIV>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
    <DIV>253</DIV>
    <DIV>sd12 is not a number</DIV>
    <DIV>-64</DIV>
    <DIV>2.25.25 is not a number</DIV>
    <DIV>2.25</DIV>