xml 文件的替换元素满足特定条件

Replacement elements of an xml file is a specific condition is met

给出以下 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>

如果属性 'a_GTINType' 的值为 99,我想删除 ProductCrossReference 元素和子元素。

我使用了以下 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>

但是 ProductCrossReference xml 元素下的子元素永远不会被删除,我不确定为什么如此任何帮助将不胜感激:

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

IIUC,你想做的:

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>

您的尝试无效,因为 Values 不是 ProductCrossReference 的子项。如果你改变它会有效:

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

至:

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

但它仍然会过于复杂。