XSLT 根据语言上下文删除节点

问题描述 投票:0回答:1

我在应用样式表来解决以下问题时遇到问题:这是我给定的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<STEP-ProductInformation>
  <Products>
    <Product ID="prd_ProdVar-286244">
      <Values>
        <ValueGroup AttributeID="tec_att_ignore_basic_data_text" ID="Y">
          <Value ID="Y" LOVQualifierID="en-US">Yes</Value>
          <Value ID="Y" LOVQualifierID="std.lang.all">Yes</Value>
          <Value ID="N" LOVQualifierID="de-DE">Ja</Value>
        </ValueGroup>
        <Value AttributeID="prd_att_description" Inherited="1" QualifierID="de-DE">
            Some text in german
        </Value>
        <Value AttributeID="prd_att_description" Inherited="5" QualifierID="en-US">
            Some text in english
        </Value>
    </Product>
  </Products>
</STEP-ProductInformation>

如您所见,我有一个

ValueGroup
(在本例中)带有
Y
N
,这意味着,我想从我的最终文件中删除与 qualifierID 相关的所有
prd_att_description
,其中有一个“ Y”代表它。如果
LOVQualifierID
等于
N
(就像德国的情况一样),则应保留该属性。

我确实尝试弄清楚并最终得到了这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@prd_att_description[../@tec_att_ignore_basic_data_text = 'Y']"/>
</xsl:stylesheet>

但这显然是错误的,因为它没有考虑上下文是什么,也不会删除节点。

预期输出:

<?xml version="1.0" encoding="UTF-8"?>

<STEP-ProductInformation>
  <Products>
    <Product ID="prd_ProdVar-286244">
      <Values>
        <ValueGroup AttributeID="tec_att_ignore_basic_data_text" ID="Y">
          <Value ID="Y" LOVQualifierID="en-US">Yes</Value>
          <Value ID="Y" LOVQualifierID="std.lang.all">Yes</Value>
          <Value ID="N" LOVQualifierID="de-DE">Ja</Value>
        </ValueGroup>
        <Value AttributeID="prd_att_description" Inherited="1" QualifierID="de-DE">
            Some text in german
        </Value>
        <!-- english part stripped/removed -->
    </Product>
  </Products>
</STEP-ProductInformation>

如您所见:由于

<Value ID="N" LOVQualifierID="de-DE">Ja</Value>
的 value-ID 在德语中是
N
,因此它被保留。英语有一个
Y
,因此被删除。

任何帮助表示赞赏

xslt
1个回答
0
投票

我无法理解您对此处需要应用的逻辑的描述。有没有可能你想做的实际上是:

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="*"/>

<xsl:key name="lov" match="ValueGroup/Value" use="@LOVQualifierID" />

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

<xsl:template match="Value[@AttributeID='prd_att_description'][key('lov', @QualifierID)/@ID='Y']"/>
    
</xsl:stylesheet>

当此样式表处理

Value
AttributeID
"prd_att_description"
时,它会在
Value
中查找相关的
ValueGroup
,使用 key
QualifierID
LOVQualifierID
属性进行匹配。如果匹配的
Value
ID
"Y"
,则删除处理后的
Value

© www.soinside.com 2019 - 2024. All rights reserved.