将所有十进制节点格式化为欧洲金额格式

时间:2014-12-19 15:04:23

标签: xslt

我有一个包含Decimal字段的XML,需要格式化为European Currency格式。 目前它有美国格式。

-<Envelope>
 -<Body>
  -<getPriceRecommendationResponse>
   -<status>
     <statusCode>Success</statusCode>
    </status>
   -<priceRecommendation>
     <tssArticleNumber>Item Number1234</tssArticleNumber>
     <compoundCode>N123</compoundCode>
     <compoundGroupCodeBucket>A</compoundGroupCodeBucket>
     <compoundCodeBucket>N123 & others</compoundCodeBucket>
     <qualityIndexCode>-</qualityIndexCode>
     <qualityIndexBucket>Std Quality</qualityIndexBucket>
     <weight>66.0341</weight>
     <weightGroupBucket>BT 123.1234 and 12345.1234</weightGroupBucket>
     <weightIsValidBucket>YES</weightIsValidBucket>
     <subGroupCode>PT</subGroupCode>
     <subGroupCodeBucket>7:B03</subGroupCodeBucket>
     <stockDistinction>MTS</stockDistinction>
     <productIdBucket>PT0401450-T46N</productIdBucket>
     <referencePrice>42.076</referencePrice>
     <averageQuantity>9</averageQuantity>
     <quantityAdjustments>0.96</quantityAdjustments>
     <highDV>2.123789</highDV>
     <averageDV>1.25141</averageDV>
     <lowDV>0.79983</lowDV>
     <additionalAdjustmentsTotal>1</additionalAdjustmentsTotal>
     <highPrice>19876.9124796544</highPrice>
     <averagePrice>12345.5481540736</averagePrice>
     <lowPrice>123344567.3075011968</lowPrice>
   </priceRecommendation>
  </getPriceRecommendationResponse>
 </Body>
</Envelope>

请帮我一个xslt,它可以格式化XML中的所有十进制节点。

以下是我已经使用的xslt。我期待一个类似的。感谢

//this has been taken from a Microsoft knowledgebase aricle and strips out the
//namespaces from an XML message using a style sheet
XslTransform :=  XslTransform.XslTransform;
XMLStyleSheet := XMLStyleSheet.XmlDocument;
XMLStyleSheet.InnerXml(
'<?xml version="1.0" encoding="UTF-8"?>'+
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxml="urn:schemas-microsoft-com:xslt">'+
'<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="yes"/>' +
'<xsl:template match="@*|node()">'+
'<xsl:copy>'+
'<xsl:apply-templates select="@*|node()"/>'+
'</xsl:copy>'+
'</xsl:template>'+
'<xsl:template match="'+OldNode+'">'+
'<xsl:variable name="oldNode" select="'+OldNode+'"/>' +
'<xsl:variable name="newNodeXml">' +
'<xsl:element name="'+NewNode+'">' +
'<xsl:copy-of select="$oldNode/@*|node()"/>' +
'<xsl:copy-of select="$oldNode/child::*"/>' +
'<xsl:copy-of select="$oldNode/@*"/>' +
'</xsl:element>' +
'</xsl:variable>' +
'<xsl:copy-of select="msxml:node-set($newNodeXml)"/>' +
'</xsl:template>' +
'</xsl:stylesheet>'
);
XslTransform.Load(XMLStyleSheet);
writer := writer.StringWriter();
XslTransform.Transform(Source, nullXsltArgumentList, writer);
Destination := Destination.XmlDocument;
Destination.InnerXml(writer.ToString());

谢谢大家的回复。我希望我能让你更清楚。对于十进制分隔符和“。”,欧洲格式为“,”。用于数字分组。而美国则相反。例如美国为1,000.156,欧洲为1.000,156。是的Rnet我已经尝试了十进制格式,它的工作正常,但问题是我需要多次使用它为Fields。我想要一个XSLT,它可以立即改变所有十进制字段的格式。我希望你明白我的观点

谢谢Michael,我已经尝试过您的代码,但收到错误。 对System.Xml.Xsl.XslTransform.Load的调用因此消息失败:表达式必须求值为节点集。

我已经改变了迈克尔给出的一点点代码,但仍然得到了同样的错误。

'<?xml-stylesheet type="text/xsl" href="decimalformat.xsl"?>'+
'<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="*"/>'+
'<xsl:decimal-format name="eu" decimal-separator="," grouping-separator="." />'+
'<!-- identity transform -->'+
'<xsl:template match="@*|node()">'+
'    <xsl:copy>'+
'         <xsl:apply-templates select="@*|node()"/>'+
'    </xsl:copy>'+
'</xsl:template>'+
'<xsl:template match="*[number()=number()]">'+
'    <xsl:copy>'+
'        <xsl:value-of select=translate("format-number(., '#.##0,##########', 'eu'), ',', '.')" />'+
'    </xsl:copy>'+
'</xsl:template>'+
'</xsl:stylesheet>'

在我的应用程序中它不起作用。我在这里找到了类似的帖子http://mikeschinkel.com/blog/gettingpastthexslterrorexpressionmustevaluatetoanodeset/#comment-484235

<xsl:value-of select="format-number(., '#.##0,##########', 'eu')" />

如果我包含上述代码,我收到错误。我也试过这种方式。

<xsl:value-of select=translate("format-number(., '#.##0,##########', 'eu'), ',', '.')" />

1 个答案:

答案 0 :(得分:1)

  

这里的问题是我需要多次使用它来为Fields。一世   想要一个更改所有十进制字段格式的XSLT   一次。

这可以通过仅匹配包含严格数值的元素的模板来完成,例如:

XSLT 1.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="*"/>

<xsl:decimal-format name="eu" decimal-separator=',' grouping-separator='.' />

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

<xsl:template match="*[number()=number()]">
    <xsl:copy>
        <xsl:value-of select="format-number(., '#.##0,##########', 'eu')" />
    </xsl:copy> 
</xsl:template>

</xsl:stylesheet>

注意

  1. 这不适用于您的输入,因为它包含未转义的&符号;

  2. 这将处理包含数字作为字符串一部分的元素,例如:
     <weightGroupBucket>BT 123.1234 and 12345.1234</weightGroupBucket>

  3. 这与货币无关。