Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 正数到负数_Xml_Xslt_Xslt 1.0_Number Formatting - Fatal编程技术网

Xml 正数到负数

Xml 正数到负数,xml,xslt,xslt-1.0,number-formatting,Xml,Xslt,Xslt 1.0,Number Formatting,如何在XSLT中检查数字(十进制或整数)。如果它是正数,我想将其转换为负数,否则我必须保持原样。我在google中验证过,我看到的是负数到正数,使用XSLT版本为1.0。请提供一些样品 抽取以下样本: <Books> <Book> <Name>NC</Name> <Price>100.50</Price> </Book> </Books> <Books> <Boo

如何在XSLT中检查数字(十进制或整数)。如果它是正数,我想将其转换为负数,否则我必须保持原样。我在google中验证过,我看到的是负数到正数,使用XSLT版本为1.0。请提供一些样品

抽取以下样本:

<Books>
 <Book>
  <Name>NC</Name>
  <Price>100.50</Price>
 </Book>
 </Books>


<Books>
 <Book>
  <Name>NC</Name>
  <Price>-200</Price>
 </Book>
 </Books>

数控
100.50
数控
-200
从负数到正数:

<xsl:value-of select="Price * (Price >= 0) - Price * not(Price >= 0)" />


我想把任何正数转换成负数,如果这个数已经是负数,我必须保持原样。

使用Dimitre Novatchev的方法找到所有正数,然后简单地在整个表达式周围加上一个
-

<xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
但在我看来,代码的作用(甚至)就不那么明显了

样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Price">
        <xsl:copy>
            <xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
        </xsl:copy>
    </xsl:template>

</xsl:transform>

XML输入

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Price">
        <xsl:copy>
            <xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
        </xsl:copy>
    </xsl:template>

</xsl:transform>
假设以下输入,其中负数和正数都存在:

<Books>
  <Price>100.50</Price>
  <Price>-133.50</Price>
  <Price>999</Price>
  <Price>-183</Price>
</Books>

100.50
-133.50
999
-183
XML输出

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Price">
        <xsl:copy>
            <xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
        </xsl:copy>
    </xsl:template>

</xsl:transform>
正如你所看到的,负数是过去的样子,正数现在是负数

<Books>
   <Price>-100.5</Price>
   <Price>-133.5</Price>
   <Price>-999</Price>
   <Price>-183</Price>
</Books>

-100.5
-133.5
-999
-183

使用Dimitre Novatchev的方法查找所有正数,然后简单地在整个表达式周围放置一个
-

<xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
但在我看来,代码的作用(甚至)就不那么明显了

样式表

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Price">
        <xsl:copy>
            <xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
        </xsl:copy>
    </xsl:template>

</xsl:transform>

XML输入

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Price">
        <xsl:copy>
            <xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
        </xsl:copy>
    </xsl:template>

</xsl:transform>
假设以下输入,其中负数和正数都存在:

<Books>
  <Price>100.50</Price>
  <Price>-133.50</Price>
  <Price>999</Price>
  <Price>-183</Price>
</Books>

100.50
-133.50
999
-183
XML输出

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Price">
        <xsl:copy>
            <xsl:value-of select="-(. * (. >= 0) - . *not(. >= 0))" />
        </xsl:copy>
    </xsl:template>

</xsl:transform>
正如你所看到的,负数是过去的样子,正数现在是负数

<Books>
   <Price>-100.5</Price>
   <Price>-133.5</Price>
   <Price>-999</Price>
   <Price>-183</Price>
</Books>

-100.5
-133.5
-999
-183

用简单的方法做怎么样

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- positive prices to negative -->
<xsl:template match="Price[. > 0]">
    <xsl:copy>
        <xsl:value-of select="-."/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,针对格式良好进行了更正:

<Books>
    <Book>
        <Name>NC</Name>
        <Price>100.50</Price>
    </Book>
    <Book>
        <Name>NC</Name>
        <Price>-200</Price>
    </Book>
</Books>

数控
100.50
数控
-200
生成以下结果

<?xml version="1.0" encoding="UTF-8"?>
<Books>
   <Book>
      <Name>NC</Name>
      <Price>-100.5</Price>
   </Book>
   <Book>
      <Name>NC</Name>
      <Price>-200</Price>
   </Book>
</Books>

数控
-100.5
数控
-200

注意:执行此操作的智慧让我无法理解。

用简单的方法做怎么样

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!-- positive prices to negative -->
<xsl:template match="Price[. > 0]">
    <xsl:copy>
        <xsl:value-of select="-."/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,针对格式良好进行了更正:

<Books>
    <Book>
        <Name>NC</Name>
        <Price>100.50</Price>
    </Book>
    <Book>
        <Name>NC</Name>
        <Price>-200</Price>
    </Book>
</Books>

数控
100.50
数控
-200
生成以下结果

<?xml version="1.0" encoding="UTF-8"?>
<Books>
   <Book>
      <Name>NC</Name>
      <Price>-100.5</Price>
   </Book>
   <Book>
      <Name>NC</Name>
      <Price>-200</Price>
   </Book>
</Books>

数控
-100.5
数控
-200

注意:执行此操作的智慧我无法理解。

请先给我一些样品。输入XML、当前尝试的XSLT代码(尝试应用找到的将负数转换为正数的方法的代码)、获得的XML输出以及预期的XML输出。谢谢为什么你不能用
“-0;-0”
作为数字格式?穆勒,请找到更新的问题。对不起,我不想包括示例。请先给我一些示例。输入XML、当前尝试的XSLT代码(尝试应用找到的将负数转换为正数的方法的代码)、获得的XML输出以及预期的XML输出。谢谢为什么你不能用
“-0;-0”
作为数字格式?穆勒,请找到更新的问题。抱歉,我不想包括示例