XSLT 2.0 - 保留默认元素,删除重复的同级元素,按属性筛选不必要的条目



Using XSLT 2.0.

我需要过滤所有具有属性@xml:lang的元素,其中属性的值不在我定义的可能值列表中。防爆允许值: x-defaultenen-USen-GB

当在任何元素上检测到@xml:lang时,如果存在x-default,则应将@xml:lang值不是x-default的任何相同类型的同级元素与元素的文本值进行比较 x-default ,如果元素文本值相同,则删除。换句话说,应该根据元素的文本值比较删除@xml:lang="x-default"的任何同级重复项。

如果可以对重复项的顺序进行排名,则奖励积分,例如始终选择x-default(如果存在(,然后是第二层(enfrru(,然后是第三层(en-ENen-GBfr-FRru-RU(,其中第一层的第二层重复项被删除,并将第三层与第二层进行比较(如果存在(, 或者第一层,以便在重复时也删除第三层。这需要动态处理,因为有许多可能的语言。

还应该考虑的一个特殊情况是,第一层(x-default(有some value,第二层(en(有some valuation,第三层(en-US(有some value。在这种情况下,没有要删除的副本,因为第二层存在,而第三层不匹配。

当前的 XSLT(不尝试删除重复项,因为我还没有找到可靠的解决方案,而且我的任何尝试都惨遭失败(。这不是我理想的 XSLT,它只是我目前所知道的最好的 XSLT,它能够过滤数据集。我的程序员希望看到所有or都更改为数组值检查,以便可以更干净地管理值,但我不确定这在 XSLT 中是否可行:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://some.namespace/uri">
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- Select everything except comment/processing instruction nodes -->
<xsl:template match="attribute()|element()|text()">
    <xsl:copy>
        <xsl:apply-templates select="attribute()|element()|text()"/>
    </xsl:copy>
</xsl:template>
<!-- Remove categories & assignments not in our whitelist -->
<xsl:template match="//*[@category-id and not(@category-id='root'
    or @category-id='men' or @category-id='men_clothing' or @category-id='men_clothing_tshirts'
    or @category-id='sales' or @category-id='sales_men' or @category-id='sales_men_tees'
    or @category-id='sales' or @category-id='sales_women' or @category-id='sales_women_tanks-teeshirts'
    or @category-id='clothing' or @category-id='clothing_teeshirts'
    or @category-id='kids' or @category-id='kids_0816'
    or @category-id='men' or @category-id='men_shoes' or @category-id='men_shoes_skate'
    or @category-id='sales' or @category-id='sales_men' or @category-id='sales _men_shoes'
    )]"/>
<!-- Remove locales not default or in whitelist -->
<xsl:template match="//*[@xml:lang and not(@xml:lang='x-default' or @xml:lang='en' or @xml:lang='en-US' or @xml:lang='en-CA' or @xml:lang='en-GB' or @xml:lang='fr' or @xml:lang='fr-FR' or @xml:lang='ru' or @xml:lang='ru-RU')]"/>
<!-- Remove empty nodes -->
<xsl:template match="*[not(normalize-space()) and not(.//@*)]"/>
</xsl:stylesheet>

下面的示例数据集。在实际数据集中,还有更多的元素,因此删除重复@xml:lang条目的逻辑不得硬编码到您可能推断的 XPath,而是对相邻分组的相同类型数据的组进行操作。

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://some.namespace/uri" catalog-id="catalog-products">
  <category category-id="sales_women">
    <display-name xml:lang="x-default"><![CDATA[Women's Sales]]></display-name>
    <display-name xml:lang="en"><![CDATA[Sales for Women]]></display-name>
    <display-name xml:lang="en-US"><![CDATA[Women's Sales]]></display-name>
  </category>
  <product product-id="111111111">
    <display-name xml:lang="x-default"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="de"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="en"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="en-US"><![CDATA[Aurora Fleece]]></display-name>
    <display-name xml:lang="es"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="fr"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="ru"><![CDATA[Aurora]]></display-name>
    <short-description xml:lang="de"><![CDATA[Aurora - Fleece-Top für Damen]]></short-description>
    <short-description xml:lang="en"><![CDATA[Aurora - Sweatshirt for women]]></short-description>
    <short-description xml:lang="x-default"><![CDATA[Aurora - Sweatshirt for women]]></short-description>
    <short-description xml:lang="en-US"><![CDATA[Snow Fleece & Softshells - Aurora Fleece]]></short-description>
    <short-description xml:lang="es"><![CDATA[Aurora - Top polar de mujer]]></short-description>
    <short-description xml:lang="fr"><![CDATA[Aurora - haut en polaire femme]]></short-description>
    <short-description xml:lang="ru"><![CDATA[Свитшот SomeBrand для девушек]]></short-description>
    <long-description xml:lang="de"><![CDATA[<p class="productLongDescriptionTitle"></p><p class="productLongDescriptionSubTitle">Composition</p><p>100 % Polyester</p>]]></long-description>
    <long-description xml:lang="en"><![CDATA[<p class="productLongDescriptionTitle"></p><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester</p>]]></long-description>
    <long-description xml:lang="x-default"><![CDATA[<p class="productLongDescriptionTitle"><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester</p>]]></long-description>
    <long-description xml:lang="en-US"><![CDATA[<p class="productLongDescriptionTitle"></p><p>The stretchy, polar fleece Aurora zip-up shields you from the elements with street-savvy style to have you standing out on the slopes and the sidewalk. Designed with a tailored fit, tech details include zippered hand warmer pockets, a lyrca binding finish, a chest pocket, flatlock seams for smooth comfort, and ergonomic seams for support. Imported. 100% polyester polar fleece.</p><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester
Polar Fleece</p>]]></long-description>
    <long-description xml:lang="es"><![CDATA[<p class="productLongDescriptionSubTitle">Composition</p><p>100% poliéster</p>]]></long-description>
    <long-description xml:lang="fr"><![CDATA[<p class="productLongDescriptionSubTitle">Composition</p><p>100 % polyester</p>]]></long-description>
    <long-description xml:lang="ru"><![CDATA[<p>Женский свитшот SomeBrand из зимней коллекции одежды 2014. Характеристики: влаговыводящая технология DRY-FLIGHT, эластичный флис из полиэстера (250 г), теплые карманы на молнии для ладошек.</p><p class="productLongDescriptionSubTitle"></p><p>100% полиэстер</p>]]></long-description>
    <!-- === PICTURES === -->
    <images>
      <image-group view-type="hi-res">
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_frt1.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_frt2.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_bck1.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
      </image-group>
    </images>
  </product>
</catalog>

例如所需的数据集:

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://some.namespace/uri" catalog-id="catalog-products">
  <category category-id="sales_women">
    <display-name xml:lang="x-default"><![CDATA[Women's Sales]]></display-name>
    <display-name xml:lang="en"><![CDATA[Sales for Women]]></display-name>
    <display-name xml:lang="en-US"><![CDATA[Women's Sales]]></display-name>
  </category>
  <product product-id="111111111">
    <display-name xml:lang="x-default"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="en-US"><![CDATA[Aurora Fleece]]></display-name>
    <short-description xml:lang="de"><![CDATA[Aurora - Fleece-Top für Damen]]></short-description>
    <short-description xml:lang="x-default"><![CDATA[Aurora - Sweatshirt for women]]></short-description>
    <short-description xml:lang="en-US"><![CDATA[Snow Fleece & Softshells - Aurora Fleece]]></short-description>
    <short-description xml:lang="es"><![CDATA[Aurora - Top polar de mujer]]></short-description>
    <short-description xml:lang="fr"><![CDATA[Aurora - haut en polaire femme]]></short-description>
    <short-description xml:lang="ru"><![CDATA[Свитшот SomeBrand для девушек]]></short-description>
    <long-description xml:lang="x-default"><![CDATA[<p class="productLongDescriptionTitle"><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester</p>]]></long-description>
    <long-description xml:lang="en-US"><![CDATA[<p class="productLongDescriptionTitle"></p><p>The stretchy, polar fleece Aurora zip-up shields you from the elements with street-savvy style to have you standing out on the slopes and the sidewalk. Designed with a tailored fit, tech details include zippered hand warmer pockets, a lyrca binding finish, a chest pocket, flatlock seams for smooth comfort, and ergonomic seams for support. Imported. 100% polyester polar fleece.</p><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester
Polar Fleece</p>]]></long-description>
    <long-description xml:lang="es"><![CDATA[<p class="productLongDescriptionSubTitle">Composition</p><p>100% poliéster</p>]]></long-description>
    <long-description xml:lang="ru"><![CDATA[<p>Женский свитшот SomeBrand из зимней коллекции одежды 2014. Характеристики: влаговыводящая технология DRY-FLIGHT, эластичный флис из полиэстера (250 г), теплые карманы на молнии для ладошек.</p><p class="productLongDescriptionSubTitle"></p><p>100% полиэстер</p>]]></long-description>
    <!-- === PICTURES === -->
    <images>
      <image-group view-type="hi-res">
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_frt1.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_frt2.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_bck1.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
      </image-group>
    </images>
  </product>
</catalog>

这里有一些你可以作为你的起点。也就是说,我相信它满足您的主要要求:

当在任何元素上检测到 @xml:lang 时,如果存在 x-default, 然后任何具有 @xml:lang 值的相同类型的同级元素 x-default 应与元素的文本值 x-default 进行比较, 如果相同的元素文本值,则删除。

为了更清楚地说明它,它删除了满足所有这三个条件的任何元素:

  1. 它有一个 XML:LANG 属性;
  2. xml:lang 属性不是"x-default";
  3. 元素的值等于名称相似的值,其 xml:lang 属性为 "x-default" 的同级元素。

XSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://some.namespace/uri">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="ns1:display-name ns1:short-description ns1:long-description ns1:alt ns1:title"/>
<xsl:key name="default-sibling" match="*[@xml:lang='x-default']" use="concat(generate-id(..), '|', local-name())" />
<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:template match="*[@xml:lang!='x-default' and .=key('default-sibling', concat(generate-id(..), '|', local-name()))]"/>
</xsl:stylesheet>

应用于示例输入,将获得以下结果

<?xml version="1.0" encoding="UTF-8"?>
<catalog xmlns="http://some.namespace/uri" catalog-id="catalog-products">
  <category category-id="sales_women">
    <display-name xml:lang="x-default"><![CDATA[Women's Sales]]></display-name>
    <display-name xml:lang="en"><![CDATA[Sales for Women]]></display-name>
  </category>
  <product product-id="111111111">
    <display-name xml:lang="x-default"><![CDATA[Aurora]]></display-name>
    <display-name xml:lang="en-US"><![CDATA[Aurora Fleece]]></display-name>
    <short-description xml:lang="de"><![CDATA[Aurora - Fleece-Top für Damen]]></short-description>
    <short-description xml:lang="x-default"><![CDATA[Aurora - Sweatshirt for women]]></short-description>
    <short-description xml:lang="en-US"><![CDATA[Snow Fleece & Softshells - Aurora Fleece]]></short-description>
    <short-description xml:lang="es"><![CDATA[Aurora - Top polar de mujer]]></short-description>
    <short-description xml:lang="fr"><![CDATA[Aurora - haut en polaire femme]]></short-description>
    <short-description xml:lang="ru"><![CDATA[Свитшот SomeBrand для девушек]]></short-description>
    <long-description xml:lang="de"><![CDATA[<p class="productLongDescriptionTitle"></p><p class="productLongDescriptionSubTitle">Composition</p><p>100 % Polyester</p>]]></long-description>
    <long-description xml:lang="en"><![CDATA[<p class="productLongDescriptionTitle"></p><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester</p>]]></long-description>
    <long-description xml:lang="x-default"><![CDATA[<p class="productLongDescriptionTitle"><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester</p>]]></long-description>
    <long-description xml:lang="en-US"><![CDATA[<p class="productLongDescriptionTitle"></p><p>The stretchy, polar fleece Aurora zip-up shields you from the elements with street-savvy style to have you standing out on the slopes and the sidewalk. Designed with a tailored fit, tech details include zippered hand warmer pockets, a lyrca binding finish, a chest pocket, flatlock seams for smooth comfort, and ergonomic seams for support. Imported. 100% polyester polar fleece.</p><p class="productLongDescriptionSubTitle">Composition</p><p>100% Polyester
Polar Fleece</p>]]></long-description>
    <long-description xml:lang="es"><![CDATA[<p class="productLongDescriptionSubTitle">Composition</p><p>100% poliéster</p>]]></long-description>
    <long-description xml:lang="fr"><![CDATA[<p class="productLongDescriptionSubTitle">Composition</p><p>100 % polyester</p>]]></long-description>
    <long-description xml:lang="ru"><![CDATA[<p>Женский свитшот SomeBrand из зимней коллекции одежды 2014. Характеристики: влаговыводящая технология DRY-FLIGHT, эластичный флис из полиэстера (250 г), теплые карманы на молнии для ладошек.</p><p class="productLongDescriptionSubTitle"></p><p>100% полиэстер</p>]]></long-description>
<!-- === PICTURES === -->
    <images>
      <image-group view-type="hi-res">
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_frt1.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_frt2.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
        <image path="catalog-products/all/default/hi-res/111111111_aurora,v_kpv0_bck1.jpg">
          <alt xml:lang="x-default"><![CDATA[Aurora 111111111]]></alt>
          <title xml:lang="x-default"><![CDATA[Aurora 111111111]]></title>
        </image>
      </image-group>
    </images>
  </product>
</catalog>

我相信你也可以扩展相同的原则来实现你的"奖励积分"和"特殊情况"(前提是你能像上面一样清楚地说明要求 - 包括共同的优先事项(。不过,您可能需要执行多个传递才能获得所有内容。

我建议您提出一个单独的问题(或搜索以前的答案(,有关基于白名单的过滤。

最新更新