【问题标题】:How to remove currency symbol from XML with XSLT?如何使用 XSLT 从 XML 中删除货币符号?
【发布时间】:2021-12-31 14:02:21
【问题描述】:

如何从 xml 代码中删除货币欧元符号?下面我粘贴输出日期xml。

XML 输入:

<products>
<product>
<sku>BTKUJ-1-2-2-3</sku>
<price>20€</price>
</product>
</products>

XSLT 1.0 示例:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:g="http://base.google.com/ns/1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="products">
<xsl:element name="products">
  <xsl:for-each select="product">
    <xsl:element name="product">
                    <xsl:element name="sku">
                        AVM0TCPD_<xsl:value-of select="id"/>
                    </xsl:element>
                    <xsl:element name="priceimp">
                        <xsl:value-of select="price"/>
                    </xsl:element>-->
    </xsl:element>
  </xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

预期结果:

<products>
<product>
<sku>BTKUJ-1-2-2-3</sku>
**<price>20</price>**
</product>
</products>

【问题讨论】:

  • 如果预期结果应该有price 元素,为什么你的代码使用&lt;xsl:element name="priceimp"&gt;?使用 translate 与例如translate(., '€', '') 从值中删除符号。

标签: xml xslt xslt-1.0


【解决方案1】:

我在你的尝试中找不到逻辑。如果您只想从 price 元素中删除欧元符号,您应该这样做:

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

<!-- identity transform -->
<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="translate(., '€', '')"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多