使用 XSLT,如何删除 XML 文档中没有属性或数据的空元素?
Using XSLT, how to remove the empty element, which does not have an attribute or data, in an XML document?
我们想从 XML
中删除 CommentLine 的空标签
<CommentLine/>
输入 XML :
<?xml version="1.0" encoding="WINDOWS-1252"?>
<SalesOrders xsd:noNamespaceSchemaLocation="SDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<OrderHeader>
<CustomerPoNumber>AB-54354</CustomerPoNumber>
</OrderHeader>
<OrderDetails>
<CommentLine>
<Comment>Ensure saddle is color coded</Comment>
<OrderLineID>OR-1810127</OrderLineID>
</CommentLine>
<CommentLine>
<Comment>EDI-001</Comment>
<OrderLineID>OR-1810128</OrderLineID>
</CommentLine>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>ABSH-SMH-12OZ-01</StockCode>
<StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
<OrderQty>1.0</OrderQty>
</StockLine>
<CommentLine>
<Comment>This is for test purpose</Comment>
<OrderLineID>OR-1810124</OrderLineID>
</CommentLine>
<CommentLine>
<Comment>EDI-SAVE</Comment>
<OrderLineID>OR-1810125</OrderLineID>
</CommentLine>
<CommentLine/>
</OrderDetails>
</Orders>
</SalesOrders>
已尝试 XML:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>
<xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<xsl:template match="@*|node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
预期输出
<?xml version="1.0" encoding="WINDOWS-1252"?> -<SalesOrders xsd:noNamespaceSchemaLocation="SDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">
-<Orders>
-<OrderHeader>
<CustomerPoNumber>AB-54354</CustomerPoNumber>
</OrderHeader>
-<OrderDetails>
-<CommentLine> <Comment>Ensure saddle is color coded</Comment>
<OrderLineID>OR-1810127</OrderLineID>
</CommentLine>
-<CommentLine> <Comment>EDI-001</Comment>
<OrderLineID>OR-1810128</OrderLineID>
</CommentLine>
-<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>ABSH-SMH-12OZ-01</StockCode>
<StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
<OrderQty>1.0</OrderQty> </StockLine>
-<CommentLine> <Comment>This is for test purpose</Comment>
<OrderLineID>OR-1810124</OrderLineID>
</CommentLine>
-<CommentLine>
<Comment>EDI-SAVE</Comment>
<OrderLineID>OR-1810125</OrderLineID>
</CommentLine>
</OrderDetails>
</Orders>
</SalesOrders>
我们必须删除完整输入中的元素 XML。
任何帮助将不胜感激!感谢您抽出宝贵时间。
此解决方案将删除所有空元素而不删除其父元素(即,如果您有 <OrderDetails><CommentLine/></OrderDetails>
,您将在结果集中获得并清空 OrderDetails
元素。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>
<xsl:template match="@*|node()[./node()]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
即我告诉系统只有 return 个有子节点的节点(无论是元素还是文本)。
<xsl:template match="@*|node()[./node()]">
演示:http://xsltransform.net/jyRYYjs
根据@Filburt 的评论,如果您还想删除在删除空子元素后变为空的元素,请遵循这些链接上的建议:
- XSL / XPath expression to check if a node contains at least one non-empty child
- How to tell using XPath if an element is present and non empty?
我们想从 XML
中删除 CommentLine 的空标签<CommentLine/>
输入 XML :
<?xml version="1.0" encoding="WINDOWS-1252"?>
<SalesOrders xsd:noNamespaceSchemaLocation="SDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<OrderHeader>
<CustomerPoNumber>AB-54354</CustomerPoNumber>
</OrderHeader>
<OrderDetails>
<CommentLine>
<Comment>Ensure saddle is color coded</Comment>
<OrderLineID>OR-1810127</OrderLineID>
</CommentLine>
<CommentLine>
<Comment>EDI-001</Comment>
<OrderLineID>OR-1810128</OrderLineID>
</CommentLine>
<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>ABSH-SMH-12OZ-01</StockCode>
<StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
<OrderQty>1.0</OrderQty>
</StockLine>
<CommentLine>
<Comment>This is for test purpose</Comment>
<OrderLineID>OR-1810124</OrderLineID>
</CommentLine>
<CommentLine>
<Comment>EDI-SAVE</Comment>
<OrderLineID>OR-1810125</OrderLineID>
</CommentLine>
<CommentLine/>
</OrderDetails>
</Orders>
</SalesOrders>
已尝试 XML:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>
<xsl:template match="@xsi:nil[.='true']" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<xsl:template match="@*|node()">
<xsl:copy copy-namespaces="no">
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
预期输出
<?xml version="1.0" encoding="WINDOWS-1252"?> -<SalesOrders xsd:noNamespaceSchemaLocation="SDOC.XSD" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">
-<Orders>
-<OrderHeader>
<CustomerPoNumber>AB-54354</CustomerPoNumber>
</OrderHeader>
-<OrderDetails>
-<CommentLine> <Comment>Ensure saddle is color coded</Comment>
<OrderLineID>OR-1810127</OrderLineID>
</CommentLine>
-<CommentLine> <Comment>EDI-001</Comment>
<OrderLineID>OR-1810128</OrderLineID>
</CommentLine>
-<StockLine>
<CustomerPoLine>9999</CustomerPoLine>
<StockCode>ABSH-SMH-12OZ-01</StockCode>
<StockDescription>SMH ABS BALANCE SHAMPOO 12OZ</StockDescription>
<OrderQty>1.0</OrderQty> </StockLine>
-<CommentLine> <Comment>This is for test purpose</Comment>
<OrderLineID>OR-1810124</OrderLineID>
</CommentLine>
-<CommentLine>
<Comment>EDI-SAVE</Comment>
<OrderLineID>OR-1810125</OrderLineID>
</CommentLine>
</OrderDetails>
</Orders>
</SalesOrders>
我们必须删除完整输入中的元素 XML。
任何帮助将不胜感激!感谢您抽出宝贵时间。
此解决方案将删除所有空元素而不删除其父元素(即,如果您有 <OrderDetails><CommentLine/></OrderDetails>
,您将在结果集中获得并清空 OrderDetails
元素。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="Windows-1252" indent="yes"/>
<xsl:template match="@*|node()[./node()]">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
即我告诉系统只有 return 个有子节点的节点(无论是元素还是文本)。
<xsl:template match="@*|node()[./node()]">
演示:http://xsltransform.net/jyRYYjs
根据@Filburt 的评论,如果您还想删除在删除空子元素后变为空的元素,请遵循这些链接上的建议:
- XSL / XPath expression to check if a node contains at least one non-empty child
- How to tell using XPath if an element is present and non empty?