使用 XSLT 跳过具有属性的节点

skip nodes having attributes using XSLT

这是我的样本 xml。我想删除节点 <shared-variation-attribute/> 在第一个变体节点中。但希望与 一起保留第二个变体节点将常规空节点移除为

 <variation-attribute>
 <variation-attribute-values/>
 </variation-attribute>

表示如何跳过具有属性的节点

<ItemList>
       <Variation>
          <attributes>
             <shared-variation-attribute/>
             <variation-attribute attribute-id="Colour" att-id="Colour">
                <display-name>Colour</display-name>
                <variation-attribute-values>
                   <variation-attribute-value value="Bronze">
                      <display-value>Bronze</display-value>
                   </variation-attribute-value>
                   <variation-attribute-value value="BRZE UNI EMBELLISSEU">
                      <display-value>BRZE UNI EMBELLISSEU</display-value>
                   </variation-attribute-value>
                </variation-attribute-values>
             </variation-attribute>
          </attributes>
          <Variants>
             <variant product-id="124451575"/>
          </Variants>
       </Variation>
       <Variation>
          <attributes>
             <shared-variation-attribute variation-attribute-id="Colour" attribute-id="Colour"/>
             <variation-attribute>
                <variation-attribute-values/>
             </variation-attribute>
          </attributes>
          <Variants>
             <variant product-id="180003356"/>
             <variant product-id="180003372"/>
             <variant product-id="180003364"/>
             <variant product-id="180003380"/>
             <variant product-id="180003398"/>
          </Variants>
       </Variation>
    </ItemList>

我试过这样的代码:

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

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

<xsl:template match="*/*[not(node())]"/>
</xsl:stylesheet>

但它正在删除所有自关闭标签。但我想保留 <variant product-id="180003372"/> 这样的节点。如何使用 XSLT

实现此目的

预期输出如下:

    <ItemList>
   <Variation>
      <attributes>
         <variation-attribute attribute-id="Colour" att-id="Colour">
            <display-name>Colour</display-name>
            <variation-attribute-values>
               <variation-attribute-value value="Bronze">
                  <display-value>Bronze</display-value>
               </variation-attribute-value>
               <variation-attribute-value value="BRZE UNI EMBELLISSEU">
                  <display-value>BRZE UNI EMBELLISSEU</display-value>
               </variation-attribute-value>
            </variation-attribute-values>
         </variation-attribute>
      </attributes>
      <Variants>
         <variant product-id="124451575"/>
      </Variants>
   </Variation>
   <Variation>
      <attributes>
         <shared-variation-attribute variation-attribute-id="Colour" attribute-id="Colour"/>
      </attributes>
      <Variants>
         <variant product-id="180003356"/>
         <variant product-id="180003372"/>
         <variant product-id="180003364"/>
         <variant product-id="180003380"/>
         <variant product-id="180003398"/>
      </Variants>
   </Variation>
</ItemList>

属性与 node() 不匹配,因此您需要将模板更改为此....

<xsl:template match="*/*[not(node()) and not(@*)]"/>

编辑:如果您想在所有后代节点都是 "empty" 的情况下删除父节点,请改用此表达式

<xsl:template match="*/*[not(descendant-or-self::*[@* or text()])]"/>

这将 select 具有至少一个属性的所有变体节点。

XDocument doc = XDocument.Load("TestFile.xml");
IEnumerable<XElement> elementList = doc.XPathSelectElements("ItemList/Variation[attributes/shared-variation-attribute[(@*)]]");

同样你可以使用

XDocument doc = XDocument.Load("TestFile.xml");
IEnumerable<XElement> elementList = doc.XPathSelectElements("ItemList/Variation[attributes/shared-variation-attribute[not(@*)]]");

到select个没有任何属性的节点。