[英]How to remove nodes from xml based on the value of a tag
Using xml and xsl, I would like to parse input xml for a particular node and tag. 使用xml和xsl,我想解析特定节点和标签的输入xml。 If the tag value starts with “z” then I would like to delete that node.
如果标签值以“ z”开头,那么我想删除该节点。 (or create new file xml file without this node)
(或在没有此节点的情况下创建新的文件xml文件)
Have attached a sample xml file for reference. 随附了示例xml文件以供参考。 Since the last node displayname starts with "z*" I would like the last node should be removed.
由于最后一个节点的显示名称以“ z *”开头,因此我希望删除最后一个节点。
"zseafood" “ zseafood”
thanks in advance. 提前致谢。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ItemSet xmlns:ns1="urn:/Items/data">
<ns1:ObjectName>com</ns1:ObjectName>
<ListOfItems>
<Item>
<ns1:Name>name1</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>fruits</ns1:DisplayName>
<ns1:ValidationErrorMsg>120</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name2</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>vegetables</ns1:DisplayName>
<ns1:ValidationErrorMsg>24.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name3</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>meat</ns1:DisplayName>
<ns1:ValidationErrorMsg>12.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name4</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>zseafood</ns1:DisplayName>
<ns1:ValidationErrorMsg>12.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
</ListOfItems>
</ItemSet>
This transformation (overriding of the identity rule ): 此转换 ( 标识规则的替代):
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="urn:/Items/data">
<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="Item[.//ns1:DisplayName[starts-with(., 'z')]]"/>
</xsl:stylesheet>
when applied on the provided XML document : 当应用于提供的XML文档时 :
<ItemSet xmlns:ns1="urn:/Items/data">
<ns1:ObjectName>com</ns1:ObjectName>
<ListOfItems>
<Item>
<ns1:Name>name1</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>fruits</ns1:DisplayName>
<ns1:ValidationErrorMsg>120</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name2</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>vegetables</ns1:DisplayName>
<ns1:ValidationErrorMsg>24.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name3</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>meat</ns1:DisplayName>
<ns1:ValidationErrorMsg>12.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name4</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>zseafood</ns1:DisplayName>
<ns1:ValidationErrorMsg>12.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
</ListOfItems>
</ItemSet>
produces the wanted, correct result : 产生想要的正确结果 :
<ItemSet xmlns:ns1="urn:/Items/data">
<ns1:ObjectName>com</ns1:ObjectName>
<ListOfItems>
<Item>
<ns1:Name>name1</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>fruits</ns1:DisplayName>
<ns1:ValidationErrorMsg>120</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name2</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>vegetables</ns1:DisplayName>
<ns1:ValidationErrorMsg>24.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
<Item>
<ns1:Name>name3</ns1:Name>
<ns1:ListOfItemDesc>
<ItemTranslation>
<ns1:DisplayName>meat</ns1:DisplayName>
<ns1:ValidationErrorMsg>12.</ns1:ValidationErrorMsg>
</ItemTranslation>
</ns1:ListOfItemDesc>
</Item>
</ListOfItems>
</ItemSet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.