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

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

问题描述

我想使用 xslt 从我的 xmls 中的所有属性中删除空格.我使用了 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天全站免登陆