xslt - Union of two node-set results leads to duplicated set member -


i'm using xslt 1.0, , need union of 2 variables means have use node-set function.

the test case below creates union of single node set contains node. since contains b, union operation should return b. new set duplicate a's.

if use xpath directly, union behaves expected. if use variables , node-set function, face unexpected case. scenario requires use node-set. i've simplified test case as can.

this xml content:

<?xml version="1.0" encoding="utf-8" ?> <root>   <event>     <id>3</id>     <eventtype>type1</eventtype>   </event>   <event>     <id>2</id>     <eventtype>type2</eventtype>     <parent>3</parent>   </event>   <event>     <id>1</id>     <eventtype>type2</eventtype>     <parent>3</parent>   </event> </root> 

and xslt:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0"                  xmlns:xsl="http://www.w3.org/1999/xsl/transform"                 xmlns:msxsl="urn:schemas-microsoft-com:xslt"                  xmlns:ext="http://exslt.org/common"                 exclude-result-prefixes="msxsl" >     <xsl:output method="xml" indent="yes"/>      <xsl:template match="/">              <xsl:variable name="t2-events">         <xsl:copy-of            select="root/event                   [eventtype = 'type2' , parent = '3']"/>       </xsl:variable>        <xsl:variable name="specific-t2">           <xsl:copy-of              select="root/event                     [eventtype = 'type2' , id = '2']"/>               </xsl:variable>        <debug>                    <results>                         <xsl:variable name="dummy"                            select="root/event                                   [eventtype = 'type2']                                    |                                    root/event[id = '2']"/>             <xsl:variable name="dummy2"                            select="ext:node-set($t2-events)                                 | ext:node-set($specific-t2) "/>              <xsl:comment>works expected</xsl:comment>             <dummy>                               <xsl:copy-of select="$dummy"/>             </dummy>             <dummy2>               <xsl:comment>there 3 elements here, should have 2</xsl:comment>                 <xsl:copy-of select="$dummy2"/>              </dummy2>                       </results>        </debug>                 </xsl:template>   </xsl:stylesheet> 

how apply union on 2 node-set() calls?

i don't think node-set() culprit here.

you've defined variables t2-events , specific-t2 using xsl:copy-of. variables should (and do) contain fresh copies (new element nodes) of nodes selected select expressions on xsl:copy-of. follows none of event elements in 2 variables identical of event elements in input, , 2 copies of event 2 made in 2 variables 2 copies, not 2 references single copy.

note when add few more variables debugging code (nice job cutting example down, way!), union happens might expect, regardless of whether use node-set() or not. (at least in xsltproc.)

after initial 2 variables, added 2 more:

<xsl:variable name="t2-events"                select="root/event                       [eventtype = 'type2' , parent = '3']"/> <xsl:variable name="specific-t2"                select="root/event                       [eventtype = 'type2' , id = '2']"/>         

then within debug element, added:

<xsl:variable name="dummy3"                select="$t2-events | $specific-t2 "/> <xsl:variable name="dummy4"                select="ext:node-set($t2-events)                      | ext:node-set($specific-t2) "/> 

and

<dummy3>   <xsl:comment>how many here?</xsl:comment>   <xsl:copy-of select="$dummy3"/> </dummy3> <dummy4>   <xsl:comment>how many here?</xsl:comment>   <xsl:copy-of select="$dummy4"/> </dummy4> 

when run resulting stylesheet xsltproc, 2 elements in dummy3 , 2 in dummy4; call node-set() made no difference.

(that said, cannot see in the description of node-set() function on exslt site says node-set() preserving node identity or not preserving it. leery of relying on either way.)


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -