xml - XSLT 2.0 - Keep default element, remove duplicate siblings, filter unnecessary entries by attribute -
xml - XSLT 2.0 - Keep default element, remove duplicate siblings, filter unnecessary entries by attribute -
using xslt 2.0.
i need filter elements have attribute @xml:lang
attribute's values not in list of possible values define. ex allowable values: x-default
,en
,en-us
,en-gb
when @xml:lang
detected on element, , if x-default
exists, sibling element of same type @xml:lang
value other x-default
should compared element's text value of x-default
, , if same element text value, removed. way, sibling duplicates of @xml:lang="x-default"
should removed, based on element's text value comparison.
bonus points if it's possible rank order of duplicates, such x-default
chosen (if exists), followed sec tier (en
, fr
, ru
), followed 3rd tier (en-en
, en-gb
, fr-fr
, ru-ru
), sec tier duplicates of first tier removed, , 3rd tier compared sec tier (if exists), or else first tier, 3rd tier removed if duplicate. need handled dynamically, there many possible languages.
a special case should considered, situation first tier (x-default
) has some value
, sec tier (en
) has some valuation
, 3rd tier (en-us
) has some value
. in situation, there's no duplicate remove, sec tier exists , 3rd tier not match it.
my current xslt (doesn't effort removing duplicates, i've not found sure-fire solution yet, , attempts on part have failed miserably). not ideal xslt, it's best know build currently, , it's able filter downwards info set. programmer in me see or
's changed array-value check values can managed more cleanly, i'm not sure if that's doable in 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 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 , 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 , 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()) , not(.//@*)]"/> </xsl:stylesheet>
example dataset below. in real dataset, there many more elements, 1 time again logic remove duplicate @xml:lang
entries must not hard-coded xpath might deduce, rather operate on groups of same-type data, grouped adjacent.
<?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 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 women]]></short-description> <short-description xml:lang="x-default"><![cdata[aurora - sweatshirt 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 elements street-savvy style have standing out on slopes , sidewalk. designed tailored fit, tech details include zippered hand warmer pockets, lyrca binding finish, chest pocket, flatlock seams smooth comfort, , ergonomic seams 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>
ex desired dataset:
<?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 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 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 elements street-savvy style have standing out on slopes , sidewalk. designed tailored fit, tech details include zippered hand warmer pockets, lyrca binding finish, chest pocket, flatlock seams smooth comfort, , ergonomic seams 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>
here's utilize starting point. is, believe satisfies primary request:
when @xml:lang detected on element, , if x-default exists, sibling element of same type @xml:lang value other x-default should compared element's text value of x-default, , if same element text value, removed.
to state more clearly, removes element satisfies of these 3 conditions:
it has xml:lang attribute; the xml:lang attribute not "x-default"; the value of element equal value of similarly-named, sibling element xml:lang attribute "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' , .=key('default-sibling', concat(generate-id(..), '|', local-name()))]"/> </xsl:stylesheet>
applied illustration input, next result obtained:
<?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 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 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 elements street-savvy style have standing out on slopes , sidewalk. designed tailored fit, tech details include zippered hand warmer pockets, lyrca binding finish, chest pocket, flatlock seams smooth comfort, , ergonomic seams 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>
i believe extend same principle accomplish "bonus points" , "special case" (provided can state requirements 1 above - including mutual priorities). may have more 1 pass them all, though.
i suggest inquire separate question (or search previous answers) regarding filtering based on whitelist.
xml xslt xpath xslt-2.0
Comments
Post a Comment