html - Dynamically Sort nodes and put delimiters where it comes using XSLT -



html - Dynamically Sort nodes and put delimiters where it comes using XSLT -

i have html file , want convert xml document using xslt..

i want:

all nodes retained. and elements sorted. and code should in dynamically. i have comma(,)between elements need handle delimiter <dl>,</dl> comes..(not comma speces want retained)

i have huge file want simple code process html nodes.here explained codeing xslt if u understand plz help me..

my html file is..

<span id="2102" class="one_biblio"> <span id="2103" class="one_section-title"><b>title</b></span> <span id="2204" class="one_authors"> <span id="2205" class="one_author">, <!--here comma arraives--> <!-- here id value misplaced --> <span id="2207" class="one_surname">surname</span>,<!--here comma arraives--> <span id="2206" class="one_given-name">givenname</span>,<!--here comma arraives--> </span> </span> <span id="2208" class="one_title"> <span id="2209" class="one_maintitle">technology</span> </span>

and want output xml file as: here attribute class value used element name. , elements sorted. , comma(,) should come within delimiter tag.

<biblio id="2102" > <section-title id="2103" ><b>title</b></section-title> <authors id="2204" > <author id="2205" > <dl>,</dl> <!--here want this--> <!-- correrct id --> <given-name id="2206" >givenname </given-name><dl>,</dl><!--here want this--> <surname id="2207" >surname</surname><dl>,</dl><!--here want this--> </author> </authors> <title id="2208" > <maintitle id="2209" >technology</maintitle> </title> </biblio>

the xslt wrote ..

<xsl:template match="*[@class]"> <xsl:element name="{substring-after(@class, 'mps_')}"> <xsl:copy-of select="@*[not(name()='class')]"/> <xsl:if test="not(current())"> <xsl:apply-templates> <xsl:sort select="@id" data-type="number"/> </xsl:apply-templates> </xsl:if> </xsl:element> </xsl:template> help me.......

this xslt 1.0 transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="vrtfpass1"> <xsl:apply-templates select="/*"/> </xsl:variable> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="node()|@*" mode="pass2"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="/"> <xsl:apply-templates mode="pass2" select="ext:node-set($vrtfpass1)/*"/> </xsl:template> <xsl:template match="*[@class]" mode="pass2"> <xsl:element name="{substring-after(@class, 'one_')}"> <xsl:copy-of select="@*[not(name()='class')]"/> <xsl:apply-templates> <xsl:sort select="@id" data-type="number"/> </xsl:apply-templates> </xsl:element> </xsl:template> <xsl:template match= "text() [../self::span[@class] or preceding-sibling::node()[1] [self::span[@class]] ] [contains(., ',')]"> <xsl:value-of select="substring-before(., ',')"/> <dl>,</dl> <xsl:value-of select="substring-after(., ',')"/> </xsl:template> </xsl:stylesheet>

when applied on provided xml document (corrected made well-formed !!!):

<span id="2102" class="one_biblio"> <span id="2103" class="one_section-title"> <b>title</b> </span> <span id="2204" class="one_authors"> <span id="2205" class="one_author">, <span id="2207" class="one_surname">surname</span>, <span id="2206" class="one_given-name">givenname</span>, </span> </span> <span id="2208" class="one_title"> <span id="2209" class="one_maintitle">technology</span> </span> </span>

produces wanted, right result:

<biblio id="2102"> <span id="2103" class="one_section-title"> <b>title</b> </span> <span id="2204" class="one_authors"> <span id="2205" class="one_author"> <dl>,</dl> <span id="2207" class="one_surname">surname</span> <dl>,</dl> <span id="2206" class="one_given-name">givenname</span> <dl>,</dl> </span> </span> <span id="2208" class="one_title"> <span id="2209" class="one_maintitle">technology</span> </span> </biblio>

explanation: two-pass transformation. first pass fixes comma, sec pass rest of processing.

html xml xslt

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -