Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

How to remove nodes from xml based on the value of a tag

Hi,

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 itself. (or create new file without this node)

Have attached a sample xml file for reference. From this I would expect the last node should be removed.

if ListOfFields/Field/ListOfFieldTranslations/FieldTranslation/DisplayName starts with z then this delete node

<ns1:Field>
      <ns1:Name>name4</ns1:Name>
      <ns1:ListOfFieldTranslations>
            <ns1:FieldTranslation>
                  <ns1:DisplayName>zIndividual</ns1:DisplayName>
                  <ns1:ValidationErrorMsg>Individual.</ns1:ValidationErrorMsg>
            </ns1:FieldTranslation>
      </ns1:ListOfFieldTranslations>
</ns1:Field>

Also could you please suggest XSLT editor for me learn or play arount? or a best XML/XLST editor for a reasonable price

Thanks in advance.
sample-xml.xml
Avatar of Bardobrave
Bardobrave
Flag of Spain image

You have to create a template that matches the value-of a node starting by "z" and do nothing when it matches
Avatar of enthuguy

ASKER

yep, thats the idea. I managed to get on how to  check the element...contains text()?

But need help on how to search the string

Below is the one got it from google search. on how to check is element emplty or not.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" 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="giro[not(nombreGiro/text())]"/>
</xsl:stylesheet>
Can any one give me the sample xslt pls
Avatar of Gertone (Geert Bormans)
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns1="urn:/crmondemand/xml/fldmgmt/data">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="node()">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="ns1:DisplayName[starts-with(normalize-space(), 'z')]"/>
</xsl:stylesheet> 

Open in new window

By far the best editor for XML and testing XSLT is Oxygen
www.oxygenxml.com
it is fairly priced, and you can use the development edition and test it for a month for free
You will be convinced after that month
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
with a slight change

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 xmlns:ns1="urn:/crmondemand/xml/fieldmanagement/data"
exclude-result-prefixes="ns1">
 <xsl:param name="testParam"/>
<xsl:output method="text" indent="no" />

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

    <xsl:template match="ns1:ListOfFields[ not (node()) ]">
        <xsl:text>false</xsl:text>
    </xsl:template>
</xsl:stylesheet>