使用基于特定属性的XSL删除空节点

时间:2017-12-20 15:24:53

标签: xml xslt

我有XSL删除整个xml的空标记,但是当属性为" DELETE"时,我想删除一个节点部分的空标记。任何帮助表示赞赏。请参阅下面的示例XML和XSL。

XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<UGNX xmlns="http://www.ems-ag.de/xmlschemas/test/UGNX.XSD">
    <Items Action="UPDATE">
        <PartyCode>TSTeb</PartyCode>
        <CatalogCode>TestCatalog</CatalogCode>
        <Item Action="ADDORUPDATE">
            <ItemCode>TST12345</ItemCode>
            <ItemType>REGULAR</ItemType>
            <ItemState>V</ItemState>
            <AttributeAttachments Action="UPDATE">
                <AttributeAttachment Action="DELETE">
                    <AttributeUID>
                        <PartyCode>TSTeb</PartyCode>    
                        <AttributeClassificationCode>TestSchema
                        </AttributeClassificationCode>
                        <AttributeCode>TestAttribute</AttributeCode>
                   </AttributeUID>
                   <AttributeValues Action="REPLACE">
                     <Value Action="ADDORUPDATE">
                        <Order>0</Order>
                        <Enumeration/>
                     </Value>
                   </AttributeValues>
               </AttributeAttachment>
            <AttributeAttachment Action="ADDORUPDATE">
                <AttributeUID>
                    <PartyCode>TSTeb</PartyCode>
                    <AttributeClassificationCode>TestSchema
                    </AttributeClassificationCode>
                    <AttributeCode>TestAttribute2</AttributeCode>
                </AttributeUID>
                <AttributeValues Action="REPLACE">
                    <Value Action="ADDORUPDATE">
                        <Order>0</Order>
                        <Translatable/>
                    </Value>
                </AttributeValues>
            </AttributeAttachment>
          </AttributeAttachments>
         </Item>
       </Items>
     </UGNX>

下面的预期结果。当AttributeAttachment&#34; Action&#34;时,删除空节点。属性是&#34; DELETE&#34;。

<?xml version="1.0" encoding="UTF-8"?>
<UGNX xmlns="http://www.ems-ag.de/xmlschemas/test/UGNX.XSD">
    <Items Action="UPDATE">
        <PartyCode>TSTeb</PartyCode>
        <CatalogCode>TestCatalog</CatalogCode>
        <Item Action="ADDORUPDATE">
            <ItemCode>TST12345</ItemCode>
            <ItemType>REGULAR</ItemType>
            <ItemState>V</ItemState>
            <AttributeAttachments Action="UPDATE">
                <AttributeAttachment Action="DELETE">
                    <AttributeUID>
                        <PartyCode>TSTeb</PartyCode>    
                        <AttributeClassificationCode>TestSchema
                        </AttributeClassificationCode>
                        <AttributeCode>TestAttribute</AttributeCode>
                   </AttributeUID>
                   <AttributeValues Action="REPLACE">
                     <Value Action="ADDORUPDATE">
                        <Order>0</Order>
                     </Value>
                   </AttributeValues>
               </AttributeAttachment>
            <AttributeAttachment Action="ADDORUPDATE">
                <AttributeUID>
                    <PartyCode>TSTeb</PartyCode>
                    <AttributeClassificationCode>TestSchema
                    </AttributeClassificationCode>
                    <AttributeCode>TestAttribute2</AttributeCode>
                </AttributeUID>
                <AttributeValues Action="REPLACE">
                    <Value Action="ADDORUPDATE">
                        <Order>0</Order>
                        <Translatable/>
                    </Value>
                </AttributeValues>
            </AttributeAttachment>
          </AttributeAttachments>
         </Item>
       </Items>
     </UGNX>

当前XSL:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0">
    <xsl:strip-space elements="*"/>

    <xsl:template match="*">
    <xsl:if test=". != '' or ./@* != ''">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()" />
        </xsl:element>
    </xsl:if>
    </xsl:template>

   <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="." />
        </xsl:attribute>
   </xsl:template>
       <xsl:template match="text() | comment() | processing-instruction()">
       <xsl:copy />
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

您应该移动当前逻辑以检查匹配“*”的模板中的“空”元素,而是使用一个与空元素匹配的单独模板,并忽略它们.....

<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<xsl:template match="*[@Action='DELETE']//*[not(* or @* or normalize-space())]" />

这里我将空定义为没有子元素,没有属性,也没有非空格文本。