XSLT 2.0 中的链接模板

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

我有一个非常简单的输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<STEP-ProductInformation>
   <Products>
      <Product>
          <Values>
            <ValueGroup AttributeID="tec_att_ignore_basic_data_text">
               <Value ID="Y" QualifierID="en-US">Yes</Value>
               <Value ID="Y" QualifierID="de-DE">Yes</Value>
            </ValueGroup>
            <ValueGroup AttributeID="prd_att_description">
               <Value QualifierID="en-US">
                   english text
               </Value>
               <Value QualifierID="de-DE">
                      german text
               </Value>
            </ValueGroup>
         </Values>
      </Product>
   </Products>
</STEP-ProductInformation>

在我的 XSL 转换中,如果属性中存在

Value
,我想删除
prd_att_description
-值组中的每个
N
ignore_basic_data_text
。 到目前为止,这工作正常,但如果所有内容都被删除(这样就没有子项),我也想删除整个 ValueGroup。

我的做法:

<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[@AttributeID='tec_att_ignore_basic_data_text']/Value" use="@QualifierID" />
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

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

它根本没有删除空的

<ValueGroup>
。我尝试使用计数元素、使用normalize-space()、使用不同的模板、使用不同的模式,但没有成功。我假设第二个模板没有运行第一个模板的结果。

有什么提示吗?

xml xslt
1个回答
0
投票

目前XSLT的版本是3.0,您可以使用

xsl:where-populated
,如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

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

  <xsl:key name="lov" match="ValueGroup[@AttributeID='tec_att_ignore_basic_data_text']/Value" use="@QualifierID" />

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

  <xsl:template match="ValueGroup[@AttributeID='prd_att_description']">
    <xsl:where-populated>
      <xsl:next-match/>
    </xsl:where-populated>
  </xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.