xsl-list
[Top] [All Lists]

[xsl] Condition problem

2009-11-04 03:37:11
Dear All,

I am having the following input:

<cross-ref>[<cr refid="MEP_L_bib32">32</cr>–<cr
refid="MEP_L_bib35">35</cr>]</cross-ref>

<cross-ref>[<cr refid="MEP_L_bib23">23</cr>–<cr
refid="MEP_L_bib25">25</cr>,<cr refid="MEP_L_bib32">32</cr>–<cr
refid="MEP_L_bib34">34</cr>,<cr refid="MEP_L_bib55">55</cr>–<cr
refid="MEP_L_bib57">57</cr>]</cross-ref>

<cross-ref>[<cr refid="MEP_L_bib23">23</cr>,<cr
refid="MEP_L_bib31">31</cr>,<cr refid="MEP_L_bib33">33</cr>,<cr
refid="MEP_L_bib36">36</cr>,<cr refid="MEP_L_bib45">45</cr>–<cr
refid="MEP_L_bib47">47</cr>]</cross-ref>

<cross-ref>[<cr refid="MEP_L_bib17">17</cr>–<cr
refid="MEP_L_bib29">29</cr>,<cr refid="MEP_L_bib43">43</cr>,<cr
refid="MEP_L_bib44">44</cr>]</cross-ref>

<cross-ref>[<cr refid="MEP_L_bib23">23</cr>,<cr
refid="MEP_L_bib24">24</cr>,<cr refid="MEP_L_bib29">29</cr>–<cr
refid="MEP_L_bib31">31</cr>,<cr refid="MEP_L_bib33">33</cr>,<cr
refid="MEP_L_bib46">46</cr>]</cross-ref>


This the code I have tried.

<xsl:template match="cross-ref">
        <xsl:when test="matches(.,'-') or matches(.,'–')">
                <ce:cross-refs>
                        <xsl:attribute name="refid">
                                <xsl:if test="cr/@refid[contains(., 'bib')] and
cr[following-sibling::node()[1]='-']">
                                        <xsl:variable name="starth"
select="cr[following-sibling::node()[1]='-']/substring-after(@refid,'MEP_L_bib')"/>
                                        <xsl:variable name="endh"
select="cr[preceding-sibling::node()[1]='-']/substring-after(@refid,'MEP_L_bib')"/>
                                        <xsl:for-each select="1 to 
count($starth)">
                                                <xsl:variable name="tv" 
select="."/>
                                                <xsl:if test="$tv != 1">
                                                        <xsl:text> </xsl:text>
                                                </xsl:if>
                                                <xsl:value-of select="for $i in 
xs:integer($starth[$tv]) to
xs:integer($endh[$tv]) return concat('bib', $i)"/>
                                        </xsl:for-each>
                                </xsl:if>
                                <xsl:if 
test="cr[following-sibling::node()[1]!='–'][preceding-sibling::node()[1]!='–'][following-sibling::node()[1]!='-'][preceding-sibling::node()[1]!='-']">
                                        <xsl:value-of
select="cr[following-sibling::node()[1]!='–'][preceding-sibling::node()[1]!='–'][following-sibling::node()[1]!='-'][preceding-sibling::node()[1]!='-'][following::text()[contains(current(),'–')]]/substring-after(@refid,'MEP_L_')"/>
                                        <xsl:text> </xsl:text>
                                </xsl:if>
                                <xsl:if test="cr/@refid[contains(., 'bib')] and
cr[following-sibling::node()[1]='–']">
                                <xsl:variable name="startn"
select="cr[following-sibling::node()[1]='–']/substring-after(@refid,'MEP_L_bib')"/>
                                <xsl:variable name="endn"
select="cr[preceding-sibling::node()[1]='–']/substring-after(@refid,'MEP_L_bib')"/>
                                <xsl:for-each select="1 to count($startn)">
                                        <xsl:variable name="tv" select="."/>
                                        <xsl:if test="$tv != 1">
                                                <xsl:text> </xsl:text>
                                        </xsl:if>
                                        <xsl:value-of select="for $i in 
xs:integer($startn[$tv]) to
xs:integer($endn[$tv]) return concat('bib', $i)"/>
                                </xsl:for-each>
                        </xsl:if>
                </xsl:attribute>
                <xsl:apply-templates/>
        </ce:cross-refs>
</xsl:template>

With the above code i am getting the following output:

<ce:cross-refs refid="bib32 bib33 bib34 bib35">[32&#x02013;35]</ce:cross-refs>
<ce:cross-refs refid="bib23 bib24 bib25 bib32 bib33 bib34 bib55 bib56
bib57">[23&#x02013;25,32&#x02013;34,55&#x02013;57]</ce:cross-refs>
<ce:cross-refs refid="bib23 bib31 bib33 bib36 bib45 bib46
bib47">[23,31,33,36,45&#x02013;47]</ce:cross-refs>
<ce:cross-refs refid="bib43 bib44 bib27 bib28
bib29">[27&#x02013;29,43,44]</ce:cross-refs>
<ce:cross-refs refid="bib23 bib24 bib33 bib46 bib29 bib30
bib31">[23,24,29&#x02013;31,33,46]</ce:cross-refs>

Required output is as follows:

<ce:cross-refs refid="bib32 bib33 bib34 bib35">[32&#x02013;35]</ce:cross-refs>
<ce:cross-refs refid="bib23 bib24 bib25 bib32 bib33 bib34 bib55 bib56
bib57">[23&#x02013;25,32&#x02013;34,55&#x02013;57]</ce:cross-refs>
<ce:cross-refs refid="bib23 bib31 bib33 bib36 bib45 bib46
bib47">[23,31,33,36,45&#x02013;47]</ce:cross-refs>
<ce:cross-refs refid="bib27 bib28 bib29 bib43
bib44">[27&#x02013;29,43,44]</ce:cross-refs>
<ce:cross-refs refid="bib23 bib24 bib29 bib30 bib31 bib33 bib46
">[23,24,29&#x02013;31,33,46]</ce:cross-refs>

Refer the last 3 lines of all the XML content.

In line 3: non-range numbers are present on left side of range pair
In line 4: non-range numbers are present on right side of range pair
In line 5: non-range numbers are present on either side of range pair

When there are non-range numbers all the non-ranges numbers are pulled
to the beginning by the current XSL code. We cannot use tokenize
because the numbers are not consistent. you may get year number in
place of number. only ID value is consistent.

I am unable to maintain the order which the present in the input XML.
Please suggest how to achieve this.

Regards,
Ganesh

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>