Home > Blockchain >  Replacement elements of an xml file is a specific condition is met
Replacement elements of an xml file is a specific condition is met

Time:03-02

Given the following xml:

<?xml version="1.0" encoding="utf-8"?>
<STEP-ProductInformation ExportTime="2022-02-28 12:28:49" ExportContext="EN_GB" ContextID="EN_GB" WorkspaceID="Approved" UseContextLocale="false">
  <Products>
    <Product ID="CS_X_990752_14" UserTypeID="CAR" ParentID="GR-45968740" Changed="true" Republished="true">
      <Name Changed="true">CS_X_990752_14</Name>
      **<ProductCrossReference ProductID="PAK_X_990752_14" Type="Packaging.CARToChild" Changed="true">
        <MetaData>
          <Value AttributeID="a_QuantityOfTheNextLowerLevelPackage" Changed="true">2</Value>
        </MetaData>
      </ProductCrossReference>**
      <Values>
         <Value AttributeID="**a_GTINType" ID="2786677" Changed="true">99**</Value>
      </Values>
    </Product>
    <Product ID="PAK_X_990752_14" UserTypeID="PAC" ParentID="GR-45968740" Changed="true" Republished="true">
      <Name Changed="true">PAK_X_990752_14</Name>
      <ProductCrossReference ProductID="EA_X_990752_UC" Type="Packaging.PACToChild" Changed="true">
        <MetaData>
          <Value AttributeID="a_QuantityOfTheNextLowerLevelPackage" Changed="true">6</Value>
        </MetaData>
      </ProductCrossReference>
      <Values>
        <Value AttributeID="a_GTINType" ID="2786677" Changed="true">14</Value>
      </Values>
    </Product>
  </Products>
</STEP-ProductInformation>

I want to remove the ProductCrossReference element and sub-elements if an attribute 'a_GTINType' has a value of 99.

I have used the following xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="STEP-ProductInformation/Products/Product/ProductCrossReference">
  <xsl:choose>
    <xsl:when test="./Values/Value[@AttributeID = 'a_GTINType'] = '99'">
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="node()"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>

But the sub elements under ProductCrossReference xml element are never removed and I'm not sure why so any help would be appreciated:

<?xml version="1.0" encoding="utf-16"?>
<STEP-ProductInformation ExportTime="2022-02-28 12:28:49" ExportContext="EN_GB" ContextID="EN_GB" WorkspaceID="Approved" UseContextLocale="false">
  <Products>
    <Product ID="CS_X_990752_14" UserTypeID="CAR" ParentID="GR-45968740" Changed="true" Republished="true">
      <Name Changed="true">CS_X_990752_14</Name>
      **<MetaData>
        <Value AttributeID="a_QuantityOfTheNextLowerLevelPackage" Changed="true">2</Value>
      </MetaData>**
      <Values>
        <Value AttributeID="a_GTINType" ID="2786677" Changed="true">99</Value>
      </Values>
    </Product>
    <Product ID="PAK_X_990752_14" UserTypeID="PAC" ParentID="GR-45968740" Changed="true" Republished="true">
      <Name Changed="true">PAK_X_990752_14</Name>
      <MetaData>
        <Value AttributeID="a_QuantityOfTheNextLowerLevelPackage" Changed="true">6</Value>
      </MetaData>
      <Values>
        <Value AttributeID="a_GTINType" ID="2786677" Changed="true">14</Value>
      </Values>
    </Product>
  </Products>
</STEP-ProductInformation>

CodePudding user response:

IIUC, you want to do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="ProductCrossReference[../Values/Value[@AttributeID='a_GTINType']='99']"/>

</xsl:stylesheet>

Your attempt does not work because Values is not a child of ProductCrossReference. It would have worked if you changed:

<xsl:when test="./Values/Value[@AttributeID = 'a_GTINType'] = '99'">

to:

<xsl:when test="../Values/Value[@AttributeID = 'a_GTINType'] = '99'">

but it still would be overcomplicated.

  • Related