xml - Aligning fields based on position using XSLT -
i have xml document records have separate fields subject tags in english , spanish. individual tags separated semicolons.
<collections> <collection name="anycollection"> <record> <field name="materia">comida; bebida; fiesta</field> <field name="subject">food; drink; party</field> <field name="recordid">abc0001</field> </record> <record> <field name="materia">comida; bebida; fiesta</field> <field name="subject">food; drink; party</field> <field name="recordid">abc0002</field> </record> <record> <field name="materia">comida; bebida; fiesta</field> <field name="subject">food; drink; party</field> <field name="recordid">abc0003</field> </record> <record> <field name="materia">fiesta; sombreros; música; baile; agua; cerveza; sopa</field> <field name="subject">party; hats; music; dance; water; beer; soup</field> <field name="recordid">abc0004</field> </record> <record> <field name="materia">comida; bebida; fiesta; sombreros; música</field> <field name="subject">food; drink; party; hats; music</field> <field name="recordid">abc0005</field> </record> <record> <field name="materia">comida; bebida; cerveza; agua</field> <field name="subject">food; drink; beer; water</field> <field name="recordid">abc0006</field> </record> <record> <field name="materia">fiesta; sombreros; música; baile; agua; cerveza</field> <field name="subject">party; hats; music; dance; water; beer</field> <field name="recordid">abc0007</field> </record> </collection> </collections>
i want able output text file contents of 2 fields grouped , aligned position can sure mirror images of each other. here current stylesheet. produces basic output want, not dynamically. basically, want able iterate through contents of each field position. i'm guessing need kind of recursive template or function, i'm having trouble figuring out.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:xs="http://www.w3.org/2001/xmlschema" exclude-result-prefixes="xs" version="2.0"> <xsl:variable name="field"> <xsl:for-each select="collections/collection[@name='anycollection']/record"> <record> <xsl:for-each select="field"> <field> <xsl:for-each select="tokenize(.[@name='materia'],';')"> <materia> <xsl:value-of select="."/> </materia> </xsl:for-each> <xsl:for-each select="tokenize(.[@name='subject'],';')"> <subject> <xsl:value-of select="."/> </subject> </xsl:for-each> </field> </xsl:for-each> </record> </xsl:for-each> </xsl:variable> <xsl:variable name="align"> <xsl:for-each select="$field/record/field"> <languagepair1> <xsl:for-each select="materia[1]"> <xsl:value-of select="."/> <xsl:text>_</xsl:text> </xsl:for-each> <xsl:for-each select="subject[1]"> <xsl:value-of select="."/> <xsl:text> </xsl:text> </xsl:for-each> </languagepair1> <languagepair2> <xsl:for-each select="materia[2]"> <xsl:value-of select="."/> <xsl:text>_</xsl:text> </xsl:for-each> <xsl:for-each select="subject[2]"> <xsl:value-of select="."/> <xsl:text> </xsl:text> </xsl:for-each> </languagepair2> </xsl:for-each> </xsl:variable> <xsl:template match="/"> <xsl:for-each-group select="$align/languagepair1" group-by="."> <xsl:value-of select="current-grouping-key()"/> </xsl:for-each-group> <xsl:for-each-group select="$align/languagepair2" group-by="."> <xsl:value-of select="current-grouping-key()"/> </xsl:for-each-group> </xsl:template> </xsl:stylesheet>
here basic output want:
comida_food bebida_drink fiesta_party sombreros_hats música_music
i need output recordid
s associated each tag, haven't been able include in stylesheet yet.
with information added, desired output this:
comida_food abc0001 abc0002 abc0003 abc0005 abc0006 bebida_drink abc0001 abc0002 abc0003 abc0005 abc0006 fiesta_party abc0001 abc0002 abc0003 abc0004 abc0005 abc0007 sombreros_hats abc0004 abc0005 abc0007 música_music abc0004 abc0005 abc0007
a nice use case fn:for-each-pair in xpath 3.0:
for-each-pair( tokenize($materia, '; '), tokenize($subject, '; '), function($x, $y) { $x || '_' || $y || '
' })
available in saxon-pe 9.5.1.1.
Comments
Post a Comment