Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

XSLT Preceding sibling issue  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've the input xml where i ve tables and notes tags. I need to display the notes which is coming between the tables tags just like that . notes can also come in different places. I've used apply templates of note before table , so its applying all the notes before table itself .I need to display the nodes tag if its coming in between the tables just like that.

INPUT:

enter code here
<step>
<note>..</note>
<para>..</para>
<table>
..
..
</table>
<note>...</note>
<table>
   ..
   ..
</table>
<table>
   ..
   ..
</table>
<note>...</note>
<text>...</text>
</step>

OUTPUT
enter code here
<fc:topic>
  <fc:subTask id="S0EC0A941" lbl="E.">
  <fc:para>..</fc:para>
  <fc:text>...</fc:text>
  <fc:note>..</fc:note>
  <fc:table>
   ..
   ..
  </fc:table>
  <fc:note>...</fc:note>
  <fc:table>
   ..
   ..
   </fc:table>
   <fc:table>
   ..
   ..
  </fc:table>
  <fc:note>...</fc:note>
  </fc:subTask>
</fc:topic>

XSLT was like this :

<xsl:template match="step">
    <fc:topic>
        <fc:subTask>
            <xsl:if test="@id">
                <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
            </xsl:if>
            <xsl:attribute name="lbl"><xsl:number format="A."/></xsl:attribute>        
            <xsl:apply-templates select="para"/>
            <xsl:apply-templates select="text"/>
            <xsl:apply-templates select="note"/>
            <xsl:apply-templates select="table"/>              
        </fc:subTask>
    </fc:topic>
</xsl:template>
but this is applying all the tables & notes. need to change XSLT to display the note which is coming in between table to display in the place only. if the Note is coming after the table or before the table , then it should come in that only . If its coming any where else then it can come after text.
 
Sheriff
Posts: 23594
48
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikki, welcome to the Ranch!

If I understand correctly, your transform is processing all of the tables and then all of the notes, so that in the output document the notes all come out as a group followed by the tables as a group.

And you want the transform to work so that the order of table and note elements is preserved in the output? If so then it looks like you need to process the node and table elements at the same time. That might look like this:



although I haven't used XSLT for a while and so I might not have the right expression to return all of the node elements and table elements.

I hope that gets you started, I suspect I might not understand your requirements completely.

By the way, when you post XML code here it helps if you use the code tags (like I did in this post). Select "XML" in the dropdown box at the top of where you're posting, then select your document with the mouse and click the Code button.
 
Ranch Hand
Posts: 710
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not exactly simple, no. The main thing is one should focus on doing the pertinent abstraction of what the structure actually is essential to the progression of the template flow that serves your needs, and then apply correctly the functionality available to xslt. Here is one of the ways of how you can do it. The mode attribute serves only as an indicator for regrouping some templates that is meant to work together for the purpose. It also involves a recursive match to the template for table... And I suppose the prefix fc is well declared.

The apply-templates part of your code is fine up to the select='note' which I add a mode to make it clear that some special processing design happens. You take out the line for select='table':

Then you set up the templates for "note" which in turn call the template for "table", all in the same mode "special".
 
g tsuji
Ranch Hand
Posts: 710
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upon re-reading the question posted, I think maybe the straight contiguity of tables after note may not be the essential condition for the processing... In that case, Paul Chapman's proposal is probably the simplest and neat solution to get by - bravo. The proper syntax is not far, it is written like this:
 
g tsuji
Ranch Hand
Posts: 710
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, typing to carelessly! my apology Paul Clapham.
 
What's brown and sticky? ... a stick. Or a tiny ad.
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!