xsl-list
[Top] [All Lists]

RE: [xsl] Select following siblings with constraints (a solution)

2009-02-06 15:29:13

Okay I reposted this so that perhaps now it can display properly.


This may be an answer or a help to the following I found at Altova Mailing List 
Archives entitled, "select immediately following siblings with constraints"
http://www.altova.com/list/xsl-list/200602/msg1000146300.html
-------------
Problem: Select immediate following siblings with constraints.
-------------
Theory solution: use the xsl:generate-id() function on the node that preceeds 
the following siblings you want to select.  Have the following siblings copy 
that id.
-------------
Working solution:  The following two xsl stylesheets (convert.xsl and 
convertstage2.xsl) put the v nodes belonging to its' chapter into the 
bookChapter node (after bookChapter was changed into a chapter node).  The 
generate-id() function was not needed because the bookChapter node already has 
unique information.
-------------
The demo.xml follows:
-------------
&#lt;?xml version="1.0" encoding="UTF-8"?>&#lt;?xml-stylesheet type="text/xsl" 
href="demo.xsl"?>
&#lt;rootNode>
&#lt;titleTop>Isaiah&#lt;/titleTop>
&#lt;title>The Book of the Prophet Isaiah&#lt;/title>
&#lt;bookChapter>Chapter 1&#lt;/bookChapter>
&#lt;overview>Judah's rebellion -- Promises and threatenings.&#lt;/overview>
&#lt;v>1 The vision of Isaiah the son of Amoz, which he saw concerning Judah 
and Jerusalem in the days of Uzziah, Jotham, Ahaz, and Hezekiah, kings of 
Judah.&#lt;/v>
&#lt;v>2 Hear, O heavens, and give ear, O earth; for the Lord hath spoken; I 
have nourished and brought up children, and they have rebelled against 
me.&#lt;/v>
&#lt;bookChapter>Chapter 2&#lt;/bookChapter>
&#lt;overview>The coming of Christ's kingdom -- Effects of God's 
majesty.&#lt;/overview>
&#lt;v>1 The word that Isaiah the son of Amoz saw concerning Judah and 
Jerusalem.&#lt;/v>
&#lt;v>2 And it shall come to pass in the last days when the mountain of the 
Lord's house shall be established in the top of the mountains, and shall be 
exalted above the hills, and all nations shall flow unto it;&#lt;/v>
&#lt;bookChapter>Chapter 3&#lt;/bookChapter>
&#lt;overview>The sin of the people -- The oppression of the rulers -- The 
judgments for pride.&#lt;/overview>
&#lt;v>1 For, behold, the Lord, the Lord of hosts, doth take away from 
Jerusalem and from Judah the stay and the staff, the whole staff of bread, and 
the whole stay of water,&#lt;/v>
&#lt;v>2 The mighty man, and the man of war, the judge, and the prophet, and 
the prudent, and the ancient,&#lt;/v>
&#lt;/rootNode>
-------------
The following are the commands (demo.sh) used to make the final document:
-------------
find . -iname "demo.xml" -exec sed -i 
"s(_at_)demo(_dot_)xsl@convert(_dot_)xsl(_at_)g" {} ';'

xsltproc demo.xml -o demo.xml --norman

find . -iname "demo.xml" -exec sed -i "s@&#lt;?xml version=\"1.0\" 
encoding=\"UTF-8\"?>@&#lt;?xml version=\"1.0\" 
encoding=\"UTF-8\"?>&#lt;?xml-stylesheet type=\"text/xsl\" 
href=\"convertstage2.xsl\"?>@g" {} ';'

xsltproc demo.xml -o demo.xml --norman

#the following sed command removes space at the beginning of lines:
sed -i 's/^[ \t]*//;s/[ \t]*$//' demo.xml;
-------------
The convert.xsl follows:
-------------
&#lt;?xml version="1.0" encoding="UTF-8"?>
&#lt;xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

&#lt;xsl:output method="xml" indent="no" version="1.0" encoding="UTF-8"/>

&#lt;xsl:template match="*">
&#lt;xsl:copy>&#lt;xsl:value-of select="."/>&#lt;/xsl:copy>
&#lt;/xsl:template>

&#lt;xsl:template match="v">
&#lt;xsl:copy>&#lt;temporaryNode>&#lt;xsl:value-of 
select="substring-after(preceding-sibling::bookChapter[1],' 
')"/>&#lt;/temporaryNode>&#lt;xsl:value-of select="."/>&#lt;/xsl:copy>
&#lt;/xsl:template>

&#lt;xsl:template match="/rootNode/bookChapter">
&#lt;chapter>
&#lt;number>&#lt;xsl:value-of select="substring-after(.,' ')"/>&#lt;/number>
&#lt;overview>&#lt;xsl:value-of 
select="following-sibling::overview[1]"/>&#lt;/overview>
&#lt;/chapter>
&#lt;/xsl:template>

&#lt;xsl:template match="/">
&#lt;rootNode>&#lt;xsl:apply-templates 
select="/rootNode/titleTop|/rootNode/title|/rootNode/bookChapter|rootNode/v"/>&#lt;/rootNode>
&#lt;/xsl:template>
&#lt;/xsl:stylesheet>
-------------
The convertstage2.xsl follows:
-------------
&#lt;?xml version="1.0" encoding="UTF-8"?>
&#lt;xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

&#lt;xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8"/>

&#lt;xsl:key name="verses" match="v" use="temporaryNode"/>

&#lt;xsl:template match="*">
&#lt;xsl:copy>&#lt;xsl:value-of select="."/>&#lt;/xsl:copy>
&#lt;/xsl:template>

&#lt;xsl:template match="/rootNode/chapter">
&#lt;xsl:copy>&#lt;xsl:apply-templates/>
&#lt;!--The node()[2] in the following line restores the original content 
because node()[1] is the temporary node used for identification (this line 
works the quickest).-->
&#lt;xsl:for-each select="key('verses', 
current()/number[1])">&#lt;v>&#lt;xsl:value-of 
select="./node()[2]"/>&#lt;/v>&#lt;/xsl:for-each>

&#lt;!--The following line works also. [position()!=1] is used to restore the 
original content which works with more than one node.-->
&#lt;!--&#lt;xsl:for-each 
select="following-sibling::v[child::ch=current()/number[1]]">&#lt;v>&#lt;xsl:copy-of
 select="current()/node()[position()!=1]"/>&#lt;/v>&#lt;/xsl:for-each>-->
&#lt;/xsl:copy>
&#lt;/xsl:template>

&#lt;xsl:template match="/">
&#lt;rootNode>&#lt;xsl:apply-templates 
select="/rootNode/titleTop|/rootNode/title|/rootNode/chapter"/>&#lt;/rootNode>
&#lt;/xsl:template>
&#lt;/xsl:stylesheet>
-------------
Final result follows (v nodes have been moved into chapter nodes):
-------------
&#lt;?xml version="1.0" encoding="UTF-8"?>
&#lt;rootNode>
&#lt;titleTop>Isaiah&#lt;/titleTop>
&#lt;title>The Book of the Prophet Isaiah&#lt;/title>
&#lt;chapter>
&#lt;number>1&#lt;/number>
&#lt;overview>Judah's rebellion -- Promises and threatenings.&#lt;/overview>
&#lt;v>1 The vision of Isaiah the son of Amoz, which he saw concerning Judah 
and Jerusalem in the days of Uzziah, Jotham, Ahaz, and Hezekiah, kings of 
Judah.&#lt;/v>
&#lt;v>2 Hear, O heavens, and give ear, O earth; for the Lord hath spoken; I 
have nourished and brought up children, and they have rebelled against 
me.&#lt;/v>
&#lt;/chapter>
&#lt;chapter>
&#lt;number>2&#lt;/number>
&#lt;overview>The coming of Christ's kingdom -- Effects of God's 
majesty.&#lt;/overview>
&#lt;v>1 The word that Isaiah the son of Amoz saw concerning Judah and 
Jerusalem.&#lt;/v>
&#lt;v>2 And it shall come to pass in the last days when the mountain of the 
Lord's house shall be established in the top of the mountains, and shall be 
exalted above the hills, and all nations shall flow unto it;&#lt;/v>
&#lt;/chapter>
&#lt;chapter>
&#lt;number>3&#lt;/number>
&#lt;overview>The sin of the people -- The oppression of the rulers -- The 
judgments for pride.&#lt;/overview>
&#lt;v>1 For, behold, the Lord, the Lord of hosts, doth take away from 
Jerusalem and from Judah the stay and the staff, the whole staff of bread, and 
the whole stay of water,&#lt;/v>
&#lt;v>2 The mighty man, and the man of war, the judge, and the prophet, and 
the prudent, and the ancient,&#lt;/v>
&#lt;/chapter>
&#lt;/rootNode>
-------------
Note:  I don't know how to make it work with a single xsl file yet.
-------------
libxslt 1.1.20 was used.
-------------

_________________________________________________________________
Windows Live™: E-mail. Chat. Share. Get more ways to connect. 
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t2_allup_explore_022009
--~------------------------------------------------------------------
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>