i have xml file format:
<?xml version="1.0" encoding="utf-8" ?> <opacresult> <valueobjects class="list"> <catalog> <notes> daily newsletter available via e-mail.
 ip authenticated. login not needed within firm. </notes> <title>health law360. </title> <url>http://health.law360.com/</url> <catalogtitles class="list"> <catalogtitle> <uuid>e5e2bc53ac1001f808cddc29f93ecad8</uuid> <timechanged class="sql-timestamp">2010-12-14 09:17:10.707</timechanged> <timeentered class="sql-timestamp">2010-12-14 09:17:10.707</timeentered> <whochanged>b23de2ffe8dd49b0b0a03d1feb3e7da2</whochanged> <whoentered>b23de2ffe8dd49b0b0a03d1feb3e7da2</whoentered> <updatesearchindex>true</updatesearchindex> <corpid>ropesgray</corpid> <cataloguuid>a20b6b4bac1001f86d28280ed0ebeb9e</cataloguuid> <type>o</type> <title>law 360. health law.</title> </catalogtitle> <catalogtitle> <uuid>e5e2bc53ac1001f808cddc299ddfe49d</uuid> <timechanged class="sql-timestamp">2010-12-14 09:17:10.707</timechanged> <timeentered class="sql-timestamp">2010-12-14 09:17:10.707</timeentered> <whochanged>b23de2ffe8dd49b0b0a03d1feb3e7da2</whochanged> <whoentered>b23de2ffe8dd49b0b0a03d1feb3e7da2</whoentered> <updatesearchindex>true</updatesearchindex> <corpid>ropesgray</corpid> <cataloguuid>a20b6b4bac1001f86d28280ed0ebeb9e</cataloguuid> <type>o</type> <title>health law 360</title> </catalogtitle> <catalogtitle> <uuid>e5e2bc53ac1001f808cddc29ec1d959b</uuid> <timechanged class="sql-timestamp">2010-12-14 09:17:10.707</timechanged> <timeentered class="sql-timestamp">2010-12-14 09:17:10.707</timeentered> <whochanged>b23de2ffe8dd49b0b0a03d1feb3e7da2</whochanged> <whoentered>b23de2ffe8dd49b0b0a03d1feb3e7da2</whoentered> <updatesearchindex>true</updatesearchindex> <corpid>ropesgray</corpid> <cataloguuid>a20b6b4bac1001f86d28280ed0ebeb9e</cataloguuid> <type>o</type> <title>health law 3 hundred sixty</title> </catalogtitle> </catalogtitles> <catalogurls class="list"/> <gmd> <uuid>f8f123acc0a816070192e296a6a71715</uuid> <timechanged class="sql-timestamp">2006-10-10 15:23:37.813</timechanged> <timeentered class="sql-timestamp">2005-01-27 00:00:00.0</timeentered> <whochanged>25db9fcd3fd247f4a20485b40cc134ad</whochanged> <whoentered>user</whoentered> <updatesearchindex>true</updatesearchindex> <corpid>ropesgray</corpid> <isruledefault>false</isruledefault> <rulename>text</rulename> <term>electronic resource</term> <prefercollection>false</prefercollection> <istechnicalmanual>false</istechnicalmanual> <sip2ismagnetic>false</sip2ismagnetic> </gmd> <issues class="list"/> </catalog> </valueobjects> </opacresult>
as can see, there other elements under sibling nodes, don't care these , want see first one.
i'm using code call template string of desired elements parameter , template loop through asterisk-delimited string parameter: (title*url*notes*)
<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:param name="columns" /> <xsl:template match="/opacresult/valueobjects"> <html> <body> <table border="1"> <!-- header row --> <tr> <xsl:call-template name="print-headers"> <xsl:with-param name="columns" select="$columns"/> </xsl:call-template> </tr> <!-- value rows --> <xsl:for-each select="catalog"> <tr> <xsl:call-template name="print-values"> <xsl:with-param name="columns" select="$columns"/> </xsl:call-template> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <!-- split string of column names , create header field names based on element names--> <xsl:template name="print-headers"> <xsl:param name="columns"/> <xsl:variable name="newlist" select="$columns"/> <xsl:variable name="first" select="substring-before($newlist, '*')" /> <xsl:variable name="remaining" select="substring-after($newlist, '*')" /> <th> <xsl:apply-templates select="catalog/*[name()=$first]"> <xsl:with-param name="header">true</xsl:with-param> </xsl:apply-templates> </th> <xsl:if test="$remaining"> <xsl:call-template name="print-headers"> <xsl:with-param name="columns" select="$remaining"/> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template name="print-values"> <xsl:param name="columns"/> <xsl:variable name="newlist" select="$columns"/> <xsl:variable name="first" select="substring-before($newlist, '*')" /> <xsl:variable name="remaining" select="substring-after($newlist, '*')" /> <td> <xsl:apply-templates select="catalog/*[name()=$first]"/> </td> <xsl:if test="$remaining"> <xsl:call-template name="print-values"> <xsl:with-param name="columns" select="$remaining"/> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template match="title"> <xsl:param name="header"/> <xsl:choose> <xsl:when test="$header='true'"> <xsl:text>title</xsl:text> </xsl:when> <xsl:otherwise> <a> <xsl:attribute name="href"> <xsl:value-of select="//*[name()='url']"/> </xsl:attribute> <xsl:value-of select="//*[name()='title']"/> </a> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="url"> <xsl:param name="header"/> <xsl:choose> <xsl:when test="$header='true'"> <xsl:text>url</xsl:text> </xsl:when> <xsl:otherwise> <a> <xsl:attribute name="href"> <xsl:value-of select="//*[name()='url']"/> </xsl:attribute> <xsl:value-of select="//*[name()='url']"/> </a> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="notes"> <xsl:param name="header"/> <xsl:choose> <xsl:when test="$header='true'"> <xsl:text>notes</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="//*[name()='notes']"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="holdingnotes"> <xsl:param name="header"/> <xsl:choose> <xsl:when test="$header='true'"> <xsl:text>holding notes</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="//*[name()='holdingnotes']"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="relatedurl"> <xsl:param name="header"/> <xsl:choose> <xsl:when test="$header='true'"> <xsl:text>related url</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="//*[name()='relatedurl']"/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="bibliographictype/hasdatafile"> <xsl:param name="header"/> <xsl:choose> <xsl:when test="$header='true'"> <xsl:text>file</xsl:text> </xsl:when> <xsl:otherwise> <xsl:value-of select="catalog/*[name()='hasdatafile']"/> </xsl:otherwise> </xsl:choose> </xsl:template>
the way can access template use //*[name()=$first] syntax extract value of element based on name $first parameter.
any appreciated. in advance. not including full xml there thousands of lines of unnecessary text.
this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:h="header" exclude-result-prefixes="h"> <h:h> <title>title</title> <url>url</url> <notes>notes</notes> </h:h> <xsl:param name="pcolumns" select="'title url notes'"/> <xsl:template match="/opacresult/valueobjects"> <html> <body> <table border="1"> <tr> <xsl:apply-templates select="document('')/*/h:h" mode="filter"/> </tr> <xsl:apply-templates/> </table> </body> </html> </xsl:template> <xsl:template match="catalog"> <tr> <xsl:call-template name="filter"/> </tr> </xsl:template> <xsl:template match="h:h/*"> <th> <xsl:value-of select="."/> </th> </xsl:template> <xsl:template match="catalog/*"> <td> <xsl:value-of select="."/> </td> </xsl:template> <xsl:template match="node()" mode="filter" name="filter"> <xsl:apply-templates select="*[contains( concat(' ',$pcolumns,' '), concat(' ',name(),' '))]"> <xsl:sort select="substring-before( concat(' ',$pcolumns,' '), concat(' ',name(),' '))"/> </xsl:apply-templates> </xsl:template> </xsl:stylesheet>
output:
<html> <body> <table border="1"> <tr> <th>title</th> <th>url</th> <th>notes</th> </tr> <tr> <td>health law360. </td> <td>http://health.law360.com/</td> <td> daily newsletter available via e-mail. ip authenticated. login not needed within firm. </td> </tr> </table> </body> </html>
note: inline data headers, pseudo sequence parameter filtering , sorting, modes not processing same element in different way processing different elements in same way also.
Comments
Post a Comment