使用XSLT 2.0。
我需要过滤所有属性@xml:lang
的元素,其中属性的值不在我定义的可能值列表中。允许值允许值:x-default
,en
,en-US
,en-GB
如果在任何元素上检测到@xml:lang
,并且存在x-default
,则应将除@xml:lang
以外的x-default
值相同类型的任何同级元素与元素的文本值x-default
,如果相同的元素文本值,则被删除。
换句话说,根据元素的文本值比较,应删除@xml:lang="x-default"
的任何兄弟重复项。
奖励积分如果可以对重复的顺序进行排名,则始终选择x-default
(如果存在),然后是第二层(en
,{{1} },fr
),然后是第三层(ru
,en-EN
,en-GB
,fr-FR
),其中第一层的第二层重复是删除,并将第三层与第二层(如果存在)或第一层进行比较,以便第三层在重复时也被删除。这需要动态处理,因为有许多可能的语言。
应该考虑的特殊情况是,第一层(ru-RU
)具有x-default
,第二层(some value
)具有en
,第三层(some valuation
)有en-US
。在这种情况下,删除没有重复,因为第二层存在且第三层不匹配。
我目前的XSLT(并没有尝试删除重复项,因为我还没有找到一个确定的解决方案,而且我的任何尝试都失败了)。这不是我理想的XSLT,它是我目前所知的最好的XSLT,它能够过滤掉数据集。我的程序员希望看到所有some value
更改为数组值检查,以便可以更清晰地管理值,但我不确定是否这样做可以在XSLT中使用:
or
下面的示例数据集。在真实数据集中,有许多更多元素,因此删除重复<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>
条目的逻辑不能硬编码到您可能推断的XPath,而是操作于相同类型的数据,相邻分组。
@xml:lang
Ex所需数据集:
<?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>
答案 0 :(得分:2)
这里有一些你可以用作起点的东西。也就是说,我相信它满足您的主要要求:
当在任何元素上检测到@xml:lang时,如果存在x-default, 然后任何相同类型的兄弟元素与@xml:lang值不同 应将x-default与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>
我相信你可以扩展相同的原则来实现你的奖励积分&#34;和特殊情况&#34; (如果你能像上面那样清楚地说明要求 - 包括共同的优先事项)。但可能必须做多次传递才能全部通过。
我建议你根据白名单提出一个单独的问题(或搜索以前的答案)。