Xml 关于xsl的使用:选择

Xml 关于xsl的使用:选择,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,我有一个xml文件,其中提到了它们的单元 <RQ>2.000</RQ> 2.000 我需要在xsl文件中检查它们的值是+还是-号。如果其为无符号,则默认为+符号。 我是用xsl:choose元素编写的,但没有成功。如果您想使用xsl:choose,您可以这样做 <xsl:template match="RQ"> <xsl:choose> <xsl:when test="number() != number()"&

我有一个xml文件,其中提到了它们的单元

    <RQ>2.000</RQ>
2.000
我需要在xsl文件中检查它们的值是+还是-号。如果其为无符号,则默认为+符号。
我是用xsl:choose元素编写的,但没有成功。

如果您想使用xsl:choose,您可以这样做

<xsl:template match="RQ">
   <xsl:choose>
      <xsl:when test="number() != number()">NaN</xsl:when>
      <xsl:when test="number() >= 0">+</xsl:when>
      <xsl:otherwise>-</xsl:otherwise>
   </xsl:choose>
</xsl:template>

楠
+
-
这也将处理不包含数字的元素。或者,您可以更好地使用模板匹配,完全不需要xsl:choose

<xsl:template match="RQ[number() != number()]">NaN</xsl:template>

<xsl:template match="RQ[number() >= 0]">+</xsl:template>

<xsl:template match="RQ">-</xsl:template>
NaN
+
-

如果可以保证
RQ
是数字的有效表示,那么只需使用:

substring('+-', 2 - (RQ > 0), 1)
<RQ>-2.000</RQ>
<RQ>2.000</RQ>
+
完整的演示

-
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:call-template name="sign">
       <xsl:with-param name="pNum" select="RQ"/>
     </xsl:call-template>
 </xsl:template>

 <xsl:template name="sign">
   <xsl:param name="pNum"/>

     <xsl:value-of select=
       "substring('+-', 2 - (RQ > 0), 1)"/>
 </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:value-of select=
       "substring('+-', 2 - (RQ > 0), 1)"/>
 </xsl:template>
</xsl:stylesheet>
当应用于本文档时:

substring('+-', 2 - (RQ > 0), 1)
<RQ>-2.000</RQ>
<RQ>2.000</RQ>
+
如果需要,这个单行XPath表达式可以封装在单独的命名模板中,从代码中的不同位置调用,如下所示:

-
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/">
     <xsl:call-template name="sign">
       <xsl:with-param name="pNum" select="RQ"/>
     </xsl:call-template>
 </xsl:template>

 <xsl:template name="sign">
   <xsl:param name="pNum"/>

     <xsl:value-of select=
       "substring('+-', 2 - (RQ > 0), 1)"/>
 </xsl:template>
</xsl:stylesheet>


但是请注意,对命名模板的调用占用了三行代码,而仅仅使用一行代码,一行

没有与之关联的xsd。它是这样工作的---+在xsl元素节点中调用此模板。你能用这些信息来修正你的问题吗?那样会使事情更清楚。谢谢