简体   繁体   中英

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. If the tag value starts with “z” then I would like to delete that node. (or create new file xml file without this node)

Have attached a sample xml file for reference. Since the last node displayname starts with "z*" I would like the last node should be removed.

"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 :

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM