英文:
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="a='';b=''"`
3. empty elements altogether (without any attributes) should be also deleted
**Sample 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>
**Expected output 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>
I'm using below XSLT 1.0 but I'm not able to get the expected result, what changes do I need to make for it to work?
<?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>
# 答案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:
<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 = &quot;a='';b=''&quot;]"/>
</xsl:stylesheet>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论