2013-04-23 51 views
0

我必须在XSL 1.0中编写一个小型的通用转换函数,在书名涉及“NC”时将负价格转为正值。价格可以发生在多个/任何级别。我必须遇到负面信号,并将其转化为任何形式的XML。请建议。车削价格为正数

XML的

<Books> 
<Book> 
    <Name>NC</Name> 
    <Price>-100.50</Price> 
</Book> 
<Book> 
    <Name>B1</Name> 
    <Pr>450.60</Pr> 
</Book> 
<Book> 
    <Name>C1</Name> 
    <Price>35.20</Price> 
</Book> 
<Book> 
    <Name>D1</Name> 
    <P>5</P> 
</Book> 
</Books> 
+0

请显示[你试过的东西](http://www.whathaveyoutried.com/) – Borodin 2013-04-23 11:22:54

回答

1

如果你想文本内容的价格与数量小于零,属于书与“NC”的名称元素。在这种情况下,您只需要进行以下模板匹配

<xsl:template match="Book[Name='NC']/Price[number(.) &lt; 0]/text()"> 

然后您可以添加代码以将负值转化为正值。

尝试以下XSLT

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

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

<xsl:template match="Book[Name='NC']/Price[number(.) &lt; 0]/text()"> 
    <xsl:value-of select="format-number(0 - number(.), '0.00')" /> 
</xsl:template> 

</xsl:stylesheet> 

当适用于您的XML,下面是输出

<Books> 
    <Book> 
     <Name>NC</Name> 
     <Price>100.50</Price> 
    </Book> 
    <Book> 
     <Name>B1</Name> 
     <Pr>450.60</Pr> 
    </Book> 
    <Book> 
     <Name>C1</Name> 
     <Price>35.20</Price> 
    </Book> 
    <Book> 
     <Name>D1</Name> 
     <P>5</P> 
    </Book> 
</Books> 

注意使用identity transform的复制现有元素。