代码之家  ›  专栏  ›  技术社区  ›  Mike Dole

删除属性值等于0且带有xls的xml节点

  •  0
  • Mike Dole  · 技术社区  · 7 年前

    我试图从一个属性值=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>
    

    <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>
    

    我感觉我就快到了,我需要什么来获得所需的输出?

    迈克

    [使用解决方案更新] 感谢Michael,这是解决我“问题”的正确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 回复  |  直到 7 年前
        1
  •  1
  •   michael.hor257k    7 年前

    这里有一种方法可以让你看到它:

    <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>
    

    还有一个:

    XSLT2.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>