我想删除空格所有使用XSLT我个XML属性。 我用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>
我用了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>
但这离开输入XML,因为它是。 我希望各级移除属性值的空间。
您可以使用翻译功能,像这样,虽然它是有意义通过调用模板来重构这个:
<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>
如果你只是想从属性值,从而消除空间,你可以只创建一个模板匹配任何属性,然后使用翻译功能。
<xsl:template match="@*">
<xsl:attribute name="{name()}">
<xsl:value-of select="translate(., ' ', '')" />
</xsl:attribute>
</xsl:template>
如果你想筛选出某些属性,您可以创建模板进行匹配,然后忽略它们。 (XSLT将首先匹配更具体的模板)
<xsl:template match="Order/@OrderKey" />
您还可以简化您的代码某种程度上被利用身份的转变,以应对现有节点。 下面是完整的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>
当应用到示例XML,以下是ouptut
<OrderList>
<Order OrderDate="26-July" OrderNo="ORDER12345" CustomertName="JOHNDOE">
<ShipAddress AddressLine="ABCColony" FirstName="John" LastName="Doe"></ShipAddress>
</Order>
</OrderList>
这种方法的好处是,它会与任何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>
编辑:如果你想只有开头和结尾的空格去掉,然后“正常化空间”是你的朋友。
<xsl:value-of select="normalize-space(.)" />
请注意:这会删除该属性内多余的空间,但(也就是双空间变得单词之间单个空格)。