使用XSLT 1.0有条件地删除XML中的空元素

huangapple go评论51阅读模式
英文:

Conditionally remove empty elements from XML using XSLT 1.0

问题

以下是您要的翻译内容:

我需要根据以下规则从示例XML中删除空元素:

1. `perm`元素不能被删除,即使它们是空的。
2. 仅在属性具有默认空值 `attr="a='';b=''";` 时删除空元素。
3. 没有任何属性的空元素也应删除。

**示例XML**

```xml
<?xml version="1.0" encoding="UTF-8"?>
<item>
    <perm desc="item_1" attr="a='';b=''">
        <content></content>
        <desc>Item 1</desc>
        <details>
            <firstname desc="Name" attr="a='';b=''">John</firstname>
            <lastname desc="Last" attr="a='';b=''"></lastname>
            <phone desc="phone" attr="a='';b='561-663-1254'"></phone>
        </details>
    </perm>
    <perm desc="item_2" attr="a='';b=''"></perm>    
</item>

预期的输出XML

<?xml version="1.0" encoding="UTF-8"?>
<item>
    <perm desc="item_1" attr="a='';b=''">
        <desc>Item 1</desc>
        <details>
            <firstname desc="Name" attr="a='';b=''">John</firstname>
            <phone desc="phone" attr="a='';b='561-663-1254'"></phone>
        </details>
    </perm>
    <perm desc="item_2" attr="a='';b=''"></perm>    
</item>

我使用以下XSLT 1.0,但无法获得预期结果,需要做哪些更改才能使其工作?

<?xml version="1.0" encoding="UTF-8" ?>
<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="@*[.='']"/>
<xsl:template match="*[not(node())]"/>
    
</xsl:stylesheet>

希望这有所帮助。如果您需要进一步的解释或修改,可以随时告诉我。

<details>
<summary>英文:</summary>

I need to remove below empty elements from the sample XML following below rules:

1. `perm` elements cannot be removed, even if they are empty
2. delete empty elements only if attr has default empty values `attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;`
3. empty elements altogether (without any attributes) should be also deleted

**Sample XML**

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;item&gt;
    	&lt;perm desc=&quot;item_1&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;
    		&lt;content&gt;&lt;/content&gt;
    		&lt;desc&gt;Item 1&lt;/desc&gt;
    		&lt;details&gt;
    			&lt;firstname desc=&quot;Name&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;John&lt;/firstname&gt;
    			&lt;lastname desc=&quot;Last&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;&lt;/lastname&gt;
    			&lt;phone desc=&quot;phone&quot; attr=&quot;a=&#39;&#39;;b=&#39;561-663-1254&#39;&quot;&gt;&lt;/phone&gt;
    		&lt;/details&gt;
    	&lt;/perm&gt;
    	&lt;perm desc=&quot;item_2&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;&lt;/perm&gt;    
    &lt;/item&gt;

**Expected output XML**

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
    &lt;item&gt;
        &lt;perm desc=&quot;item_1&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;        
    		&lt;desc&gt;Item 1&lt;/desc&gt;
            &lt;details&gt;
                &lt;firstname desc=&quot;Name&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;John&lt;/firstname&gt;            
                &lt;phone desc=&quot;phone&quot; attr=&quot;a=&#39;&#39;;b=&#39;561-663-1254&#39;&quot;&gt;&lt;/phone&gt;
            &lt;/details&gt;
        &lt;/perm&gt;
        &lt;perm desc=&quot;item_2&quot; attr=&quot;a=&#39;&#39;;b=&#39;&#39;&quot;&gt;&lt;/perm&gt;    
    &lt;/item&gt;

I&#39;m using below XSLT 1.0 but I&#39;m not able to get the expected result, what changes do I need to make for it to work?

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
    &lt;xsl:stylesheet version=&quot;1.0&quot;
     xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
     &lt;xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/&gt;
     &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
    
    &lt;xsl:template match=&quot;@*|node()&quot;&gt;
        &lt;xsl:copy&gt;
            &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
        &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
    
    &lt;xsl:template match=&quot;@*[.=&#39;&#39;]&quot;/&gt;
    &lt;xsl:template match=&quot;*[not(node())]&quot;/&gt;
    
    &lt;/xsl:stylesheet&gt;



</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

不是所有的条件都清楚,但以下内容应该能够帮助您更进一步:

<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(self::perm)][not(@|node())] | *[not(self::perm)][not(node()) and @attr = 'a=''';b=''' ]"/>

</xsl:stylesheet>



<details>
<summary>英文:</summary>

Not all conditions are clear but the following should get you further than your intent:

    &lt;xsl:stylesheet version=&quot;1.0&quot;
     xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
     &lt;xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/&gt;
     &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
    
    &lt;xsl:template match=&quot;@*|node()&quot;&gt;
        &lt;xsl:copy&gt;
            &lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
        &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
    
    &lt;xsl:template match=&quot;*[not(self::perm)][not(@*|node())] | *[not(self::perm)][not(node()) and @attr = &amp;quot;a=&#39;&#39;;b=&#39;&#39;&amp;quot;]&quot;/&gt;
    
    &lt;/xsl:stylesheet&gt;

</details>



huangapple
  • 本文由 发表于 2023年6月8日 23:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定