You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by enpysoft <np...@gmail.com> on 2009/07/29 13:26:51 UTC

Selecting distinct values from different node in XPATH 1.0

Hi Experts,
          I have to get the list of the distinct attribute values across
different node level. To help you understand my requirement, i am herewith
listed with a sample XML file

My XML file looks like this

<validation>
 <report>
   <script city="NY">
          <case name="ramesh" status="BE"/>
          <case name="rajesh" status="B.Com" />
   </script>
   <script city="AL">
          <case name="ramesh" status="Law" />
   </script>
 </report>
</validation>

Now i have to get only the values as ramesh and rajesh and i should not get
ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
distinct-values function. I earlier tried using to fetch distinct values to
a variable using the below script in xslt and it is not working.

<xsl:variable name="uniquevalues"
select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
/>

The above code snippet is not working. Kindly let me know what has to be
done to get my expected output.


thanks in advance,
npselvan
-- 
View this message in context: http://www.nabble.com/Selecting-distinct-values-from-different-node-in-XPATH-1.0-tp24717305p24717305.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Re: Selecting distinct values from different node in XPATH 1.0

Posted by Michael Ludwig <ml...@as-guides.com>.
enpysoft schrieb:
> Hi Experts,
>           I have to get the list of the distinct attribute values
> across different node level.

Finding unique values is an application of grouping, and grouping in
XSLT 1.0 is usually done using a technique called "Muenchian grouping".
Googling for that should yield lots of examples.

> <validation>
>  <report>
>    <script city="NY">
>           <case name="ramesh" status="BE"/>
>           <case name="rajesh" status="B.Com" />
>    </script>
>    <script city="AL">
>           <case name="ramesh" status="Law" />
>    </script>
>  </report>
> </validation>

It basically requires setting up an <xsl:key> for the nodes you're
interested in, which in your case might be:

   <xsl:key name="case-by-name" match="case" use="@name"/>

> <xsl:variable name="uniquevalues"
> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
> />

Just make that:

   <xsl:variable name="all-names" select="/*/report/script/case/@name"/>

You can then do:

<xsl:for-each select="$all-names">
   <xsl:if test="generate-id() =
                 generate-id( key( 'case-by-name', .)[1] )">
     <your-processing-here/>
   </xsl:if>
</xsl:for-each>

This looks a bit odd, but it is not too difficult to understand: It
checks to see if the current node is identical with the first node
in document order having the current node's string value. This ensures
each value is processed only once.

Michael Ludwig


Re: Selecting distinct values from different node in XPATH 1.0

Posted by Christoffer Dam Bruun <cd...@flyingpigs.dk>.
The code works for me (after fixing a syntax error so the scriptreport 
was valid xml).

Output produced:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Demo Application </title>
<body>
<table width="100%" cellpadding="5" cellspacing="1" border="1">
<tr>
<td> Test </td><td>login</td>
</tr>
</table>
</body>
</head>
</html>


It seems there is something wrong with your setup - not your code

Best regards,
Christoffer Dam Bruun

enpysoft skrev:
> Hi Chris, 
>             I here with attaching a sample XML file and the XSLT file which
> is same like what i am using. Hope this could help to debug the issue. If i
> am wrong please correct me.
>
> http://www.nabble.com/file/p24721882/report.xml report.xml 
> http://www.nabble.com/file/p24721882/scriptreport.xsl scriptreport.xsl 
>
> Thanks in Advance,
> npselvan.
>
>
>
> Christoffer Dam Bruun wrote:
>   
>> That kind of errors are never nice....
>>
>> If the problem is that set:distinct is returning a resulttree-fragment
>> then
>> it might help wrapping the variable in common:node-set()  before using 
>> it e.g.;
>> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>> version="1.0"
>>     xmlns:common="http://exslt.org/common"
>>     ... >
>> ...
>> <xsl:for-each
>> select="common:node-set($var-returned-by-distinct-function)">
>> ....
>> <xsl:for-each>
>>
>>
>>
>> Best regards
>> Christoffer Dam Bruun
>>
>> enpysoft skrev:
>>     
>>> Hi Chris,
>>>             I am new to XSLT programming. Thanks for your reference. I
>>> did
>>> as like you said. But i get some exception like this
>>> java.lang.reflect.InvocationTargetException when i traverse loop the
>>> variable i stored the distinct values. So can you suggest me if i did
>>> anything wrong. But when i created the variable and not referred anywhere
>>> the variable it is not causing the problem. This exception is thrown when
>>> i
>>> am using the variable to loop through using xsl:for-each. Can you help me
>>> how to resolve this kind of issue.
>>>
>>> Thanks in Advance,
>>> npselvan 
>>>
>>>
>>> Christoffer Dam Bruun wrote:
>>>   
>>>       
>>>> Hi Npselval,
>>>>
>>>> Is it impossible for you to use the exslt.org extensions (they are part 
>>>> of xalan you should just declare the namespace).
>>>> They contain an implementation of the distinct function. Example:
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>>>> version="1.0"
>>>>     xmlns:set="http://exslt.org/sets">
>>>>
>>>> <xsl:variable name="uniquevalues"
>>>> select="set:distinct(/validation/report/script/case/@name)"
>>>> />
>>>>
>>>> ...
>>>>
>>>>
>>>> Best regards
>>>> Christoffer Dam Bruun
>>>>
>>>> enpysoft skrev:
>>>>     
>>>>         
>>>>> Hi Experts,
>>>>>           I have to get the list of the distinct attribute values
>>>>> across
>>>>> different node level. To help you understand my requirement, i am
>>>>> herewith
>>>>> listed with a sample XML file
>>>>>
>>>>> My XML file looks like this
>>>>>
>>>>> <validation>
>>>>>  <report>
>>>>>    <script city="NY">
>>>>>           <case name="ramesh" status="BE"/>
>>>>>           <case name="rajesh" status="B.Com" />
>>>>>    </script>
>>>>>    <script city="AL">
>>>>>           <case name="ramesh" status="Law" />
>>>>>    </script>
>>>>>  </report>
>>>>> </validation>
>>>>>
>>>>> Now i have to get only the values as ramesh and rajesh and i should not
>>>>> get
>>>>> ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
>>>>> distinct-values function. I earlier tried using to fetch distinct
>>>>> values
>>>>> to
>>>>> a variable using the below script in xslt and it is not working.
>>>>>
>>>>> <xsl:variable name="uniquevalues"
>>>>> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
>>>>> />
>>>>>
>>>>> The above code snippet is not working. Kindly let me know what has to
>>>>> be
>>>>> done to get my expected output.
>>>>>
>>>>>
>>>>> thanks in advance,
>>>>> npselvan
>>>>>   
>>>>>       
>>>>>           
>>>>     
>>>>         
>>>   
>>>       
>>
>>     
>
>   


Re: Selecting distinct values from different node in XPATH 1.0

Posted by enpysoft <np...@gmail.com>.
Hi Chris, 
            I here with attaching a sample XML file and the XSLT file which
is same like what i am using. Hope this could help to debug the issue. If i
am wrong please correct me.

http://www.nabble.com/file/p24721882/report.xml report.xml 
http://www.nabble.com/file/p24721882/scriptreport.xsl scriptreport.xsl 

Thanks in Advance,
npselvan.



Christoffer Dam Bruun wrote:
> 
> That kind of errors are never nice....
> 
> If the problem is that set:distinct is returning a resulttree-fragment
> then
> it might help wrapping the variable in common:node-set()  before using 
> it e.g.;
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> version="1.0"
>     xmlns:common="http://exslt.org/common"
>     ... >
> ...
> <xsl:for-each
> select="common:node-set($var-returned-by-distinct-function)">
> ....
> <xsl:for-each>
> 
> 
> 
> Best regards
> Christoffer Dam Bruun
> 
> enpysoft skrev:
>> Hi Chris,
>>             I am new to XSLT programming. Thanks for your reference. I
>> did
>> as like you said. But i get some exception like this
>> java.lang.reflect.InvocationTargetException when i traverse loop the
>> variable i stored the distinct values. So can you suggest me if i did
>> anything wrong. But when i created the variable and not referred anywhere
>> the variable it is not causing the problem. This exception is thrown when
>> i
>> am using the variable to loop through using xsl:for-each. Can you help me
>> how to resolve this kind of issue.
>>
>> Thanks in Advance,
>> npselvan 
>>
>>
>> Christoffer Dam Bruun wrote:
>>   
>>> Hi Npselval,
>>>
>>> Is it impossible for you to use the exslt.org extensions (they are part 
>>> of xalan you should just declare the namespace).
>>> They contain an implementation of the distinct function. Example:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>>> version="1.0"
>>>     xmlns:set="http://exslt.org/sets">
>>>
>>> <xsl:variable name="uniquevalues"
>>> select="set:distinct(/validation/report/script/case/@name)"
>>> />
>>>
>>> ...
>>>
>>>
>>> Best regards
>>> Christoffer Dam Bruun
>>>
>>> enpysoft skrev:
>>>     
>>>> Hi Experts,
>>>>           I have to get the list of the distinct attribute values
>>>> across
>>>> different node level. To help you understand my requirement, i am
>>>> herewith
>>>> listed with a sample XML file
>>>>
>>>> My XML file looks like this
>>>>
>>>> <validation>
>>>>  <report>
>>>>    <script city="NY">
>>>>           <case name="ramesh" status="BE"/>
>>>>           <case name="rajesh" status="B.Com" />
>>>>    </script>
>>>>    <script city="AL">
>>>>           <case name="ramesh" status="Law" />
>>>>    </script>
>>>>  </report>
>>>> </validation>
>>>>
>>>> Now i have to get only the values as ramesh and rajesh and i should not
>>>> get
>>>> ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
>>>> distinct-values function. I earlier tried using to fetch distinct
>>>> values
>>>> to
>>>> a variable using the below script in xslt and it is not working.
>>>>
>>>> <xsl:variable name="uniquevalues"
>>>> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
>>>> />
>>>>
>>>> The above code snippet is not working. Kindly let me know what has to
>>>> be
>>>> done to get my expected output.
>>>>
>>>>
>>>> thanks in advance,
>>>> npselvan
>>>>   
>>>>       
>>>
>>>     
>>
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Selecting-distinct-values-from-different-node-in-XPATH-1.0-tp24717305p24721882.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Re: Selecting distinct values from different node in XPATH 1.0

Posted by enpysoft <np...@gmail.com>.
Chris, 
          Again i am facing the same problem and throwing the same
exception.

Thanks,
npselvan


Christoffer Dam Bruun wrote:
> 
> That kind of errors are never nice....
> 
> If the problem is that set:distinct is returning a resulttree-fragment
> then
> it might help wrapping the variable in common:node-set()  before using 
> it e.g.;
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> version="1.0"
>     xmlns:common="http://exslt.org/common"
>     ... >
> ...
> <xsl:for-each
> select="common:node-set($var-returned-by-distinct-function)">
> ....
> <xsl:for-each>
> 
> 
> 
> Best regards
> Christoffer Dam Bruun
> 
> enpysoft skrev:
>> Hi Chris,
>>             I am new to XSLT programming. Thanks for your reference. I
>> did
>> as like you said. But i get some exception like this
>> java.lang.reflect.InvocationTargetException when i traverse loop the
>> variable i stored the distinct values. So can you suggest me if i did
>> anything wrong. But when i created the variable and not referred anywhere
>> the variable it is not causing the problem. This exception is thrown when
>> i
>> am using the variable to loop through using xsl:for-each. Can you help me
>> how to resolve this kind of issue.
>>
>> Thanks in Advance,
>> npselvan 
>>
>>
>> Christoffer Dam Bruun wrote:
>>   
>>> Hi Npselval,
>>>
>>> Is it impossible for you to use the exslt.org extensions (they are part 
>>> of xalan you should just declare the namespace).
>>> They contain an implementation of the distinct function. Example:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>>> version="1.0"
>>>     xmlns:set="http://exslt.org/sets">
>>>
>>> <xsl:variable name="uniquevalues"
>>> select="set:distinct(/validation/report/script/case/@name)"
>>> />
>>>
>>> ...
>>>
>>>
>>> Best regards
>>> Christoffer Dam Bruun
>>>
>>> enpysoft skrev:
>>>     
>>>> Hi Experts,
>>>>           I have to get the list of the distinct attribute values
>>>> across
>>>> different node level. To help you understand my requirement, i am
>>>> herewith
>>>> listed with a sample XML file
>>>>
>>>> My XML file looks like this
>>>>
>>>> <validation>
>>>>  <report>
>>>>    <script city="NY">
>>>>           <case name="ramesh" status="BE"/>
>>>>           <case name="rajesh" status="B.Com" />
>>>>    </script>
>>>>    <script city="AL">
>>>>           <case name="ramesh" status="Law" />
>>>>    </script>
>>>>  </report>
>>>> </validation>
>>>>
>>>> Now i have to get only the values as ramesh and rajesh and i should not
>>>> get
>>>> ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
>>>> distinct-values function. I earlier tried using to fetch distinct
>>>> values
>>>> to
>>>> a variable using the below script in xslt and it is not working.
>>>>
>>>> <xsl:variable name="uniquevalues"
>>>> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
>>>> />
>>>>
>>>> The above code snippet is not working. Kindly let me know what has to
>>>> be
>>>> done to get my expected output.
>>>>
>>>>
>>>> thanks in advance,
>>>> npselvan
>>>>   
>>>>       
>>>
>>>     
>>
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Selecting-distinct-values-from-different-node-in-XPATH-1.0-tp24717305p24721316.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Re: Selecting distinct values from different node in XPATH 1.0

Posted by Christoffer Dam Bruun <cd...@flyingpigs.dk>.
That kind of errors are never nice....

If the problem is that set:distinct is returning a resulttree-fragment then
it might help wrapping the variable in common:node-set()  before using 
it e.g.;
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"
    xmlns:common="http://exslt.org/common"
    ... >
...
<xsl:for-each select="common:node-set($var-returned-by-distinct-function)">
....
<xsl:for-each>



Best regards
Christoffer Dam Bruun

enpysoft skrev:
> Hi Chris,
>             I am new to XSLT programming. Thanks for your reference. I did
> as like you said. But i get some exception like this
> java.lang.reflect.InvocationTargetException when i traverse loop the
> variable i stored the distinct values. So can you suggest me if i did
> anything wrong. But when i created the variable and not referred anywhere
> the variable it is not causing the problem. This exception is thrown when i
> am using the variable to loop through using xsl:for-each. Can you help me
> how to resolve this kind of issue.
>
> Thanks in Advance,
> npselvan 
>
>
> Christoffer Dam Bruun wrote:
>   
>> Hi Npselval,
>>
>> Is it impossible for you to use the exslt.org extensions (they are part 
>> of xalan you should just declare the namespace).
>> They contain an implementation of the distinct function. Example:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
>> version="1.0"
>>     xmlns:set="http://exslt.org/sets">
>>
>> <xsl:variable name="uniquevalues"
>> select="set:distinct(/validation/report/script/case/@name)"
>> />
>>
>> ...
>>
>>
>> Best regards
>> Christoffer Dam Bruun
>>
>> enpysoft skrev:
>>     
>>> Hi Experts,
>>>           I have to get the list of the distinct attribute values across
>>> different node level. To help you understand my requirement, i am
>>> herewith
>>> listed with a sample XML file
>>>
>>> My XML file looks like this
>>>
>>> <validation>
>>>  <report>
>>>    <script city="NY">
>>>           <case name="ramesh" status="BE"/>
>>>           <case name="rajesh" status="B.Com" />
>>>    </script>
>>>    <script city="AL">
>>>           <case name="ramesh" status="Law" />
>>>    </script>
>>>  </report>
>>> </validation>
>>>
>>> Now i have to get only the values as ramesh and rajesh and i should not
>>> get
>>> ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
>>> distinct-values function. I earlier tried using to fetch distinct values
>>> to
>>> a variable using the below script in xslt and it is not working.
>>>
>>> <xsl:variable name="uniquevalues"
>>> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
>>> />
>>>
>>> The above code snippet is not working. Kindly let me know what has to be
>>> done to get my expected output.
>>>
>>>
>>> thanks in advance,
>>> npselvan
>>>   
>>>       
>>
>>     
>
>   


Re: Selecting distinct values from different node in XPATH 1.0

Posted by enpysoft <np...@gmail.com>.
Hi Chris,
            I am new to XSLT programming. Thanks for your reference. I did
as like you said. But i get some exception like this
java.lang.reflect.InvocationTargetException when i traverse loop the
variable i stored the distinct values. So can you suggest me if i did
anything wrong. But when i created the variable and not referred anywhere
the variable it is not causing the problem. This exception is thrown when i
am using the variable to loop through using xsl:for-each. Can you help me
how to resolve this kind of issue.

Thanks in Advance,
npselvan 


Christoffer Dam Bruun wrote:
> 
> Hi Npselval,
> 
> Is it impossible for you to use the exslt.org extensions (they are part 
> of xalan you should just declare the namespace).
> They contain an implementation of the distinct function. Example:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> version="1.0"
>     xmlns:set="http://exslt.org/sets">
> 
> <xsl:variable name="uniquevalues"
> select="set:distinct(/validation/report/script/case/@name)"
> />
> 
> ...
> 
> 
> Best regards
> Christoffer Dam Bruun
> 
> enpysoft skrev:
>> Hi Experts,
>>           I have to get the list of the distinct attribute values across
>> different node level. To help you understand my requirement, i am
>> herewith
>> listed with a sample XML file
>>
>> My XML file looks like this
>>
>> <validation>
>>  <report>
>>    <script city="NY">
>>           <case name="ramesh" status="BE"/>
>>           <case name="rajesh" status="B.Com" />
>>    </script>
>>    <script city="AL">
>>           <case name="ramesh" status="Law" />
>>    </script>
>>  </report>
>> </validation>
>>
>> Now i have to get only the values as ramesh and rajesh and i should not
>> get
>> ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
>> distinct-values function. I earlier tried using to fetch distinct values
>> to
>> a variable using the below script in xslt and it is not working.
>>
>> <xsl:variable name="uniquevalues"
>> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
>> />
>>
>> The above code snippet is not working. Kindly let me know what has to be
>> done to get my expected output.
>>
>>
>> thanks in advance,
>> npselvan
>>   
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Selecting-distinct-values-from-different-node-in-XPATH-1.0-tp24717305p24719834.html
Sent from the Xalan - J - Users mailing list archive at Nabble.com.


Re: Selecting distinct values from different node in XPATH 1.0

Posted by Christoffer Dam Bruun <cd...@flyingpigs.dk>.
Hi Npselval,

Is it impossible for you to use the exslt.org extensions (they are part 
of xalan you should just declare the namespace).
They contain an implementation of the distinct function. Example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"
    xmlns:set="http://exslt.org/sets">

<xsl:variable name="uniquevalues"
select="set:distinct(/validation/report/script/case/@name)"
/>

...


Best regards
Christoffer Dam Bruun

enpysoft skrev:
> Hi Experts,
>           I have to get the list of the distinct attribute values across
> different node level. To help you understand my requirement, i am herewith
> listed with a sample XML file
>
> My XML file looks like this
>
> <validation>
>  <report>
>    <script city="NY">
>           <case name="ramesh" status="BE"/>
>           <case name="rajesh" status="B.Com" />
>    </script>
>    <script city="AL">
>           <case name="ramesh" status="Law" />
>    </script>
>  </report>
> </validation>
>
> Now i have to get only the values as ramesh and rajesh and i should not get
> ramesh two times. As i use only XPATH 1.0 and XSLT 1.0 i cannot use
> distinct-values function. I earlier tried using to fetch distinct values to
> a variable using the below script in xslt and it is not working.
>
> <xsl:variable name="uniquevalues"
> select="/validation/report/script/case[not(@name=predicate-sibling::validation/report/script/case/@name)]/@name"
> />
>
> The above code snippet is not working. Kindly let me know what has to be
> done to get my expected output.
>
>
> thanks in advance,
> npselvan
>