如何使用xslt删除所有属性值中的空格?

SMA*_*AVA 3 xslt attributes xslt-1.0 removing-whitespace

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

<OrderList>
<Order OrderDate="26-July" OrderNo="ORDER 12345"
 CustomertName="JOHN DOE" OrderKey="ORDKEY12345">
<ShipAddress AddressLine="ABC Colony" FirstName="John" LastName="Doe "/>
</Order>
</OrderList>
Run Code Online (Sandbox Code Playgroud)

和xsl我曾经摆脱属性中的空格,如CustomertName="JOHN DOE":

<?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> 
Run Code Online (Sandbox Code Playgroud)

但这会使输入xml保持不变.我想从所有级别的属性值中删除空格.

Stu*_*tLC 8

您可以像这样使用translate函数,尽管使用调用模板重构它是有意义的:

<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>
Run Code Online (Sandbox Code Playgroud)

  • @SMA_JAVA只删除前导和尾随空格,使用*normalize-space(@CustomertName)* (2认同)

Tim*_*m C 7

如果您只想从属性值中删除空格,您可以创建一个匹配任何属性的模板,然后使用translate函数.

<xsl:template match="@*">
   <xsl:attribute name="{name()}">
      <xsl:value-of select="translate(., ' ', '')" />
   </xsl:attribute>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

如果要过滤掉某些属性,可以创建要匹配的模板,然后忽略它们.(XSLT将首先匹配更具体的模板)

<xsl:template match="Order/@OrderKey" />
Run Code Online (Sandbox Code Playgroud)

您还可以通过使用身份转换来处理现有节点来稍微简化代码.这是完整的XSLT

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

   <xsl:template match="@*">
      <xsl:attribute name="{name()}">
         <xsl:value-of select="translate(., ' ', '')" />
      </xsl:attribute>
   </xsl:template>

   <xsl:template match="Order/@OrderKey" />

   <xsl:template match="node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

应用于示例XML时,以下是ouptut

<OrderList>
  <Order OrderDate="26-July" OrderNo="ORDER12345" CustomertName="JOHNDOE">
    <ShipAddress AddressLine="ABCColony" FirstName="John" LastName="Doe"></ShipAddress>
  </Order>
</OrderList>
Run Code Online (Sandbox Code Playgroud)

这种方法的优点是它可以与任何XML文档一起使用.

如果您只想为特定元素执行此操作,请尝试使用此XSLT,它明确匹配您想要的属性(并丢弃所有其他元素)

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

   <xsl:template match="Order/@OrderDate|Order/@OrderNo|Order/@CustomertName|ShipAddress/@AddressLine|ShipAddress/@FirstName|ShipAddress/@LastName">
      <xsl:attribute name="{name()}">
         <xsl:value-of select="translate(., ' ', '')"/>
      </xsl:attribute>
   </xsl:template>

   <xsl:template match="@*"/>

   <xsl:template match="node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

编辑:如果你只想删除前导和尾随空格,那么'normalize-space'就是你的朋友.

<xsl:value-of select="normalize-space(.)" />
Run Code Online (Sandbox Code Playgroud)

请注意,这将删除属性中的多余空格(也就是说,双空格成为单词之间的单个空格).