如何使用xslt删除所有属性值中的空格? [英] How do I remove spaces in all attribute values using xslt?

查看:69
本文介绍了如何使用xslt删除所有属性值中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用xslt从我的xml中的所有属性中删除空格.我使用了strip-space,但是从节点中删除了空格. 我输入的xml是:

I want to remove spaces from all attributes in my xmls using xslt. I used strip-space, but that removes the spaces from nodes. My input xml is:

<OrderList>
<Order OrderDate="26-July" OrderNo="ORDER 12345"
 CustomertName="JOHN DOE" OrderKey="ORDKEY12345">
<ShipAddress AddressLine="ABC Colony" FirstName="John" LastName="Doe "/>
</Order>
</OrderList>

和我用来消除诸如CustomertName="JOHN DOE"之类的属性中的空格的xsl是:

and the xsl I used to get rid of the spaces in the attributes like CustomertName="JOHN DOE" is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/> 
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates/>
<OrderList>
    <xsl:for-each select="OrderList/Order">
        <xsl:element name="Order">
            <xsl:copy-of select="@OrderDate"/>
            <xsl:copy-of select="@OrderNo"/>
            <xsl:copy-of select="@CustomertName"/>

            <!-- ShipAddress begins -->
            <xsl:element name="ShipAddress">                                           
                <xsl:copy-of select="ShipAddress/@AddressLine"/>
                <xsl:copy-of select="ShipAddress/@FirstName"/>
                <xsl:copy-of select="ShipAddress/@LastName"/>                       
            </xsl:element>
        </xsl:element>
    </xsl:for-each>             
</OrderList>
</xsl:template>
</xsl:stylesheet> 

但这将输入xml保留为原样.我想从所有级别的属性值中删除空格.

But this leaves the input xml as it was. I want to remove the spaces from the attribute values at all levels.

推荐答案

您可以像这样使用translate函数,尽管可以通过调用模板对其进行重构:

You can use the translate function, like so, although it would make sense to refactor this with a call template:

<xsl:attribute name="OrderDate">
    <xsl:value-of select="translate(@OrderDate, ' ','')"/>
</xsl:attribute>
<xsl:attribute name="OrderNo">
    <xsl:value-of select="translate(@CustomertName, ' ','')"/>
</xsl:attribute>
<xsl:attribute name="CustomertName">
    <xsl:value-of select="translate(@CustomertName, ' ','')"/>
</xsl:attribute>

这篇关于如何使用xslt删除所有属性值中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆