简体   繁体   English

XSLT:我们可以使用abs值吗?

[英]XSLT: can we use abs value?

I would like to know if in XLST can we use the math:abs(...) ? 我想知道在XLST中我们是否可以使用数学:abs(...)? I saw this somewhere but it does not work . 我在某个地方看到了它,但它不起作用。 I have something like: 我有类似的东西:

<tag>
  <xsl:value-of select="./product/blablaPath"/>
</tag>

I tried to do something like: 我尝试过这样的事情:

<tag>
  <xsl:value-of select="math:abs(./product/blablaPath)"/>
</tag>

but does not work. 但不起作用。 I'm using java 1.6 language. 我正在使用java 1.6语言。

Here is a single XPath expression implementing the abs() function: 这是一个实现abs()函数的XPath表达式

($x >= 0)*$x - not($x >= 0)*$x

This evaluates to abs($x) . 这评估为 abs($x)

Here is a brief demonstration of this in action : 以下是对此的简要演示

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

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

 <xsl:template match="text()">
  <xsl:param name="x" select="."/>
  <xsl:value-of select=
  "($x >= 0)*$x - not($x >= 0)*$x"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied to the following XML document: 当此转换应用于以下XML文档时:

<t>
  <num>-3</num>
  <num>0</num>
  <num>5</num>
</t>

the wanted, correct result (abs() on every number) is produced : 在想,正确的结果 (ABS()上的每个数字) 产生

<t>
  <num>3</num>
  <num>0</num>
  <num>5</num>
</t>

abs() is trivial enough. abs()是微不足道的。 Implemented in pure XSLT it would look like this: 在纯XSLT中实现它看起来像这样:

<xsl:template name="abs">
  <xsl:param name="number">

  <xsl:choose>
    <xsl:when test="$number &gt;= 0">
      <xsl:value-of select="$number" />
    <xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$number * -1" />
    </xsl:otherwise>
  </xsl:if>
</xsl:template>

in your context you would invoke it like this: 在你的上下文中,你会像这样调用它:

<tag>
  <xsl:call-template name="abs">
    <xsl:with-param name="number" select="number(product/blablaPath)" />
  </xsl:call-template>
</tag>

Anotherway: 其他方式:

(2*($x >= 0) - 1)*$x

When $x is positive, the test returns "true", so 2*true-1 returns 1, so final result is $x. 当$ x为正数时,测试返回“true”,因此2 * true-1返回1,因此最终结果为$ x。 When $x is negative, the test returns "false", so 2*false-1 returns -1, so final result is -$x. 当$ x为负数时,测试返回“false”,因此2 * false-1返回-1,因此最终结果为 - $ x。

Using 运用

2*(any-test-here)-1
is a good way to have +1 when test is true , and -1 when false . 是有+1时,测试结果为-1 的好方法,而且。

A very simple solution is to use the XSL 1.0 translate function. 一个非常简单的解决方案是使用XSL 1.0转换功能。 Ie

<xsl:value-of select="translate($x, '-', '')/>

math:abs is not built in to XSLT or XPATH. math:abs不是内置于XSLT或XPATH中。 It is an XSLT extension, provided by the runtime you are transforming with. 它是一个XSLT扩展,由您正在转换的运行时提供。

Here is an article about .NET xslt extensions . 这是一篇关于.NET xslt扩展的文章。

Here is one for Java (Xalan). 这是Java (Xalan)的一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在Java中将xslt转换与SAX解析器一起使用? - can we use xslt transfrmation with SAX parser in Java? 弹簧启动; 我们可以在 Java 枚举中使用 @Value - SpringBoot; Can we use @Value in Java Enums 我们可以使用嵌入在 avro 记录中的模式来反序列化值吗? - can we use the schema embedded in the avro record to deserialize the value? java中的处理程序和BiHandler:我们可以使用BiHandler的“返回”值吗? - handlers and BiHandler in java : can we use the "returned" value of a BiHandler? Lombok我们可以将@Builder和@Value一起用于单个类 - Lombok Can we use @Builder and @Value together on a single class 我们可以使用Linq从Java中的集合对象获取价值吗? - Can we use Linq for geting value from collection object in java? 我们可以使用“ catch”来处理具有特定值的OutOfBoundsException吗? - Can we use “catch” to handle OutOfBoundsException with specific value? 在hibernate中存储键值对 我们可以使用地图吗? - storing key value pairs in hibernate | can we use map? Java中浮点值的abs函数 - abs function for float value in java 如何在Java中使用XSLT 2.0和XSLT 3.0? - How can I use XSLT 2.0 and XSLT 3.0 in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM