2014-10-29 5 views
0
<category link-id="681" link-handle="package-products" value="Package products"> 
     <entry id="1077" images="1" products="1" brands="1"> 
      <sub-category> 
       <item handle="pens">Pens</item> 
       <item handle="refills-pens">Refills : Pens</item> 
      </sub-category> 
     </entry> 
     <entry id="1075" images="1" products="1" brands="1"> 
      <sub-category> 
       <item handle="pencil">Pencil</item> 
       <item handle="refills-pencil">Refills : Pencil</item> 
      </sub-category> 
     </entry> 
     <entry id="1073" images="1" products="1" brands="1"> 
      <sub-category> 
       <item handle="pencil">Pencil</item> 
       <item handle="refills-pencil">Refills : Pencil</item> 
      </sub-category> 
     </entry> 
     <entry id="1050" images="1" products="1" brands="1"> 
      <sub-category> 
       <item handle="marker">Marker</item> 
       <item handle="refills-marker">Refills : Marker</item> 
      </sub-category> 
     </entry> 

Я хочу удалить дублирование 3-го блока из подкатегории для вывода. Пожалуйста, помогите мне в этом. Я хочу выход как:Удалить дубликаты элементов подкатегории из xml

Pens 

Refills : Pens 

Pencil 

Refills : Pencils 

Marker 

Refills : Marker 
+0

Вы можете разместить XSLT (то, что вы пытались), и всегда после простого и полного XML для тестирования (здесь, он не очень хорошо сформирован XML). –

+1

Какая версия XSLT? –

+0

@ DanielHaley: версия 1.0 XSLT –

ответ

1

Попробуйте это: (XSLT-версия 2)

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:key name="kItemHandle" match="entry" use="sub-category/item"/> 

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

<xsl:template match="category"> 
    <xsl:for-each select="entry[count(. | key('kItemHandle', sub-category/item[1])[1])=1]"> 
     <xsl:value-of select="sub-category/item[1]"/> 
     <xsl:text>&#xa;</xsl:text> 
     <xsl:value-of select="sub-category/item[2]"/><xsl:text>&#xa;</xsl:text> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 
+0

Спасибо за тонну за помощь! Теперь у меня есть вопрос, почему и в чем смысл этого утверждения. « « Также у меня есть несколько категорий, которые означают больше 3, они повторяются, так что вы можете предложить, как сделать это динамически жаль задавать такие вопросы, но я действительно новый для XSLT. –

+0

предназначено для символов NEWLINE (линия break). Для нескольких категорий, вы можете попробовать сначала по своему усмотрению, теперь «ENTRY» имеет многократное право, как будто это изменяется на «CATOGORY». Я предложу в ближайшее время. Если возможно, поднимите новый вопрос. –

2

Вот вариант, который использует XSLT 2.0 группировки (xsl:for-each-group). Опция, предложенная Rudramuni TP, фактически может использоваться как опция XSLT 1.0.

XML Input

<category link-id="681" link-handle="package-products" value="Package products"> 
    <entry id="1077" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pens">Pens</item> 
      <item handle="refills-pens">Refills : Pens</item> 
     </sub-category> 
    </entry> 
    <entry id="1075" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pencil">Pencil</item> 
      <item handle="refills-pencil">Refills : Pencil</item> 
     </sub-category> 
    </entry> 
    <entry id="1073" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pencil">Pencil</item> 
      <item handle="refills-pencil">Refills : Pencil</item> 
     </sub-category> 
    </entry> 
    <entry id="1050" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="marker">Marker</item> 
      <item handle="refills-marker">Refills : Marker</item> 
     </sub-category> 
    </entry> 
</category> 

XSLT 2,0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/*"> 
     <xsl:for-each-group select="entry/sub-category" group-by="item[1]"> 
      <xsl:apply-templates select="current-group()[1]"/>     
     </xsl:for-each-group> 
    </xsl:template> 

    <xsl:template match="item"> 
     <xsl:value-of select="concat(.,'&#xA;&#xA;')"/> 
    </xsl:template> 

</xsl:stylesheet> 

Выход

Pens 

Refills : Pens 

Pencil 

Refills : Pencil 

Marker 

Refills : Marker 
+0

+1 для очень простого и совершенного кода. –

0

Это для Multiple 'catogory':

Входной XML:

<root> 
<category link-id="681" link-handle="package-products" value="Package products"> 
    <entry id="1077" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pens">Pens</item> 
      <item handle="refills-pens">Refills : Pens</item> 
     </sub-category> 
    </entry> 
    <entry id="1075" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pencil">Pencil</item> 
      <item handle="refills-pencil">Refills : Pencil</item> 
     </sub-category> 
    </entry> 
    <entry id="1073" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pencil">Pencil</item> 
      <item handle="refills-pencil">Refills : Pencil</item> 
     </sub-category> 
    </entry> 
    <entry id="1050" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="marker">Marker</item> 
      <item handle="refills-marker">Refills : Marker</item> 
     </sub-category> 
    </entry> 
</category> 

<category link-id="681" link-handle="package-products" value="Package products"> 
    <entry id="1077" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pens">Pens</item> 
      <item handle="refills-pens">Refills : Pens</item> 
     </sub-category> 
    </entry> 
    <entry id="1075" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pencil">Pencil</item> 
      <item handle="refills-pencil">Refills : Pencil</item> 
     </sub-category> 
    </entry> 
    <entry id="1073" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="pencil">Pencil</item> 
      <item handle="refills-pencil">Refills : Pencil</item> 
     </sub-category> 
    </entry> 
    <entry id="1050" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="marker">Marker</item> 
      <item handle="refills-marker">Refills : Marker</item> 
     </sub-category> 
    </entry> 
    <entry id="1052" images="1" products="1" brands="1"> 
     <sub-category> 
      <item handle="paper">Paper</item> 
      <item handle="refills-paper">Refills : Paper</item> 
     </sub-category> 
    </entry> 
</category> 
</root> 

XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:key name="kItemHandle" match="entry" use="sub-category/item"/> 

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

<xsl:template match="root"> 
    <xsl:for-each select="category/entry[count(. | key('kItemHandle', sub-category/item)[1])=1]"> 
     <xsl:value-of select="sub-category/item[1]"/> 
     <xsl:text>&#xa;</xsl:text> 
     <xsl:value-of select="sub-category/item[2]"/><xsl:text>&#xa;</xsl:text> 
    </xsl:for-each> 
</xsl:template> 
</xsl:stylesheet> 

Выход:

Ручки
Refills: Ручки
Карандаш
Refills: Карандаш
Маркер
Refills: Маркер
бумага
Refills: Бумага

Смежные вопросы