xslt 1.0 - find unique values by several criteria

半腔热情 提交于 2019-12-13 01:23:22

问题


I have following XML:

<library>
<elements>
    <element name="books">
        <property name="author">A</property>
        <property name="select">true</property>
    </element>
    <element name="books">
        <property name="author">B</property>
        <property name="select">false</property>
    </element>  
    <element name="books">
        <property name="author">C</property>
        <property name="select">true</property>
    </element>  
    <element name="books">
        <property name="author">A</property>
        <property name="select">true</property>
    </element>  
</elements>
</library>

I need to get output of all elements with name="books", which are selected (selected = true) and unique by author name. Must use xslt 1.0.

Expected result: author: A author: C

Must output data only for authors A and C.

Thanks in advance!


回答1:


 not(.=preceding::*) 

to retrieve the unique values of the given xpath in for loop

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output method="text"/>

     <xsl:template match="/">           
        <ul> 
      <xsl:for-each select="//elements/element[@name = 'books' and property[@name = 'select' and .='true'] ]/property[@name = 'author' and not(.=preceding::*)]">
    <li>
      <xsl:value-of select="concat('author :',.)"/>
        </li>   
      </xsl:for-each>            
</ul>
  </xsl:template>

</xsl:stylesheet>

OUTPUT XML :

author :A author :C



来源:https://stackoverflow.com/questions/35888478/xslt-1-0-find-unique-values-by-several-criteria

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!