Home > Software design >  XSLT script for selecting a particular node and remove attribute
XSLT script for selecting a particular node and remove attribute

Time:01-11

I have a xml file, I need to update xml file based on selecting a particular node and removing certain attributes from it.

In the xml file, I need to select node whose validationscenario name = "ABC test" then I need to remove validation whose abc_name is "data_005" and "data_006" only from ABC_test(not from XYZ test)

Input xml:

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns="http://abcxyz.com/abc/config" version="0.37">
<validationscenarios>
      <validationscenarioslist>
         <validationscenario>
            <name>ABC test</name>
            <validations>
               <validation abc_name="data_001" levels="1"/>
               <validation abc_name="data_002" levels="2.4"/>
               <validation abc_name="data_003" levels="6.0"/>
               <validation abc_name="data_004" levels="4.0"/>
               <validation abc_name="data_005" levels="2,3"/>
               <validation abc_name="data_006" levels="3.5,4.5"/>
            </validations>
         </validationscenario>
         <validationscenario>
            <name>XYZ test</name>
            <validations>
               <validation abc_name="data_001" levels="3.5"/>
               <validation abc_name="data_002" levels="5.5"/>
               <validation abc_name="data_005" levels="6.5"/>
               <validation abc_name="data_006" levels="4.5"/>
            </validations>
         </validationscenario>
</validationscenarioslist>
</validationscenarios>
</config>

I tried writting the script and reached mid way, I am able to select the node but I am not able to remove the attribute.

my xslt (incomplete) script

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cc="http://abcxyz.com/abc/config" xmlns="http://abcxyz.com/abc/config" version="1.0">
  <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no" standalone="no"/>
  <xsl:strip-space elements="*"/>

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

<xsl:template match=" /cc:config/cc:validationscenarios/cc:validationscenarioslist/cc:validationscenario/cc:name[contains(.,'ABC test')]">

// not working here //cc:validations/cc:validation[@abc_name='data_005'] //

</xsl:transform>

CodePudding user response:

AFAICT, you want to do:

XSLT 3.0.

<xsl:stylesheet version="3.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://abcxyz.com/abc/config">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:mode on-no-match="shallow-copy"/>

<xsl:template match="validationscenario[name='ABC test']/validations/validation[@abc_name=('data_005', 'data_006')]"/>

</xsl:stylesheet>

CodePudding user response:

in stead of:

<xsl:template match=" /cc:config/cc:validationscenarios/cc:validationscenarioslist/cc:validationscenario/cc:name[contains(.,'ABC test')]">

use:

<xsl:template match="cc:validationscenario[name='ABC test']/cc:validations/cc:validation[@abc_name[.='data_005' or .= 'data_006']]"/>
  • Related