使用xls删除属性值等于0的xml节点

时间:2018-12-10 14:48:55

标签: xslt xslt-2.0

我正在尝试从其中属性值之一= 0(数量)的xml中“剥离”节点。 我的输入xml是:

<?xml version="1.0" encoding="utf-16"?>
<StockAdjustments xmlns="http://example.com/IWS/StockAdjustments">
    <StockAdjustment Article="VL04604" Client="BHP" Status="PAXD" Unit="M2" Quantity="-96" ReasonCode="TELVERSCHI" Batch="VK00427062" HostLocation="BHP" />
    <StockAdjustment Article="VL04604" Client="BHP" Status="" Unit="M2" Quantity="0" ReasonCode="TELVERSCHI" Batch="VK00427062" HostLocation="BHP" />
</StockAdjustments>

请求/预期结果是:

<?xml version="1.0" encoding="utf-16"?>
<StockAdjustments xmlns="http://example.com/IWS/StockAdjustments">
    <StockAdjustment Article="VL04604" Client="BHP" Status="PAXD" Unit="M2" Quantity="-96" ReasonCode="TELVERSCHI" Batch="VK00427062" HostLocation="BHP" />
</StockAdjustments>

经过数小时的命名空间苦苦挣扎之后,我“生产”了以下xslt:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:iws="http://example.com/IWS/StockAdjustments"
    xmlns="http://example.com/IWS/StockAdjustments"
    excluxxde-result-prefixes="iws">
        <xsl:output method="xml" indent="yes" />
        <xsl:template match="/iws:StockAdjustments/iws:StockAdjustment[@Quantity != '0']">
           <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
         </xsl:template>
    </xsl:stylesheet>

哪个给我

<?xml version="1.0" encoding="UTF-8"?>
    <StockAdjustment xmlns="http://example.com/IWS/StockAdjustments">VL04604BHPPAXDM2-96TELVERSCHIVK00427062BHP</StockAdjustment>

我有快要走了的感觉,我需要什么才能获得所需的输出?

迈克

[使用解决方案更新] 感谢迈克尔,这是我的“问题”的正确xslt

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:iws="http://example.com/IWS/StockAdjustments">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/iws:StockAdjustments">
    <xsl:copy>
        <xsl:copy-of select="iws:StockAdjustment[@Quantity != 0]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

这是一种查看方式:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://example.com/IWS/StockAdjustments">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="StockAdjustment[@Quantity=0]"/>

</xsl:stylesheet>

这是另一个:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://example.com/IWS/StockAdjustments">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/StockAdjustments">
    <xsl:copy>
        <xsl:copy-of select="StockAdjustment[@Quantity != 0]"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>