Home > other >  xpath predicate to limit xml nodes display
xpath predicate to limit xml nodes display

Time:01-30

Back with that rather old (now) question about limiting xml nodes display at some point. The xml file is like this:

<persns>
<prsn> 
  <fname>Smith</fname> 
  <lname>Milton</lname> 
  <age>44</age> 
  <addrss>5th summer st, mntb</addrss>
  <city>Portland</city>
 </prsn>
 <prsn> 
  <fname>Ken</fname> 
  <lname>Jackson</lname> 
  <age>37</age> 
  <addrss>19th Penfield ave, brtcl</addrss>
  <city>Kelowna</city>
 </prsn>
 <prsn> 
  <fname>Susan</fname> 
  <lname>Arkland</lname> 
  <age>48</age> 
  <addrss>34th Mansfield st, sgtp</addrss>
  <city>Raleigh</city>
 </prsn>
 <prsn> 
  <fname>George</fname> 
  <lname>Bond</lname> 
  <age>35</age> 
  <addrss>5th drive, mntb</addrss>
  <city>Albany</city>
 </prsn>
 <prsn> 
  <fname>Ron</fname> 
  <lname>Davis</lname> 
  <age>37</age> 
  <addrss>12th Greenfield ave, brtcl</addrss>
  <city>Pheonix</city>
 </prsn>
</persns>

And through some xslt transformation the displaying I want to be this way:

<table border="1">
<tr><th>fname</th><td>Smith</td><td>Ken</td><td>Susan</td></tr>
<tr><th>lname</th><td>Milton</td><td>Jackson</td><td>Arkland</td></tr>
<tr><th>age</th><td>44</td><td>37</td><td>48</td></tr>
<tr><th>addrss</th><td>5th summer st, mntb</td><td>19th Penfield ave, brtcl</td><td>34th Mansfield st, sgtp</td></tr>
 <tr><th>city</th><td>Portland</td><td>Kelowna</td><td>Raleigh</td></tr>
</table>
       Fig.1 Required Display

I somehow manage to almost displaying it but not quite .. So my relevant xsl stylesheet is as follows:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<!-- page count starts from 1 -->
<xsl:param name="pag" select="1"/>
<!-- frme=frame - three records per page -->
<xsl:param name="frme" as="xs:integer" select="3"/>

<xsl:template match="persns">
 <xsl:for-each select="prsn[position()&gt;=($pag*$frme) 1 and position()&lt;=($frme   ($pag*$frme))]">
 <xsl:variable name="rec" select="."/>

<table border="1">
<xsl:for-each select="*/name()">
<xsl:variable name="i" select="position()"/>
<tr><th><xsl:value-of select="."/></th>
<xsl:for-each select="$rec">
 <td><xsl:value-of select="*[$i]"/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>

So through the above stylesheet, the transformation is showing someth. like:

<table border="1">
<tr><th>fname</th><td>Smith</td></tr>
<tr><th>lname</th><td>Milton</td></tr>
<tr><th>age</th><td>44</td></tr>
<tr><th>addrss</th><td>5th summer st, mntb</td></tr>
<tr><th>city</th><td>Portland</td></tr>
<tr><th>fname</th><td>Ken</td></tr>
<tr><th>lname</th><td>Jackson</td></tr>
<tr><th>age</th><td>37</td></tr>
<tr><th>addrss</th><td>19th Penfield ave, brtcl</td></tr>
<tr><th>city</th><td>Kelowna</td></tr>
<tr><th>fname</th><td>Susan</td></tr>
<tr><th>lname</th><td>Arkland</td></tr>
<tr><th>age</th><td>48</td></tr>
<tr><th>addrss</th><td>34th Mansfield st, sgtp</td>
</tr>
<tr><th>city</th><td>Raleigh</td></tr>
</table>
   Fig.2 Actual Displaying
          (not wanted) 

So please you guys help me for displaying the above xml as shown in fig.1. I'm truly sorry for asking actually the same question three times, but I'm in very much need of this to work as I requested. Thanks alot in advance

CodePudding user response:

The question is not entirely clear. I suppose you want to do something like:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:param name="pag" select="1"/>
<xsl:param name="frme" select="3"/>

<xsl:template match="/persns">
    <xsl:variable name="start" select="($pag - 1) * $frme   1" />
    <xsl:variable name="rec" select="prsn[position() ge $start and position() lt $start   $frme]"/>
    <table border="1">
        <xsl:for-each select="$rec[1]/*">
            <xsl:variable name="i" select="position()"/>
            <tr>
                <th>
                    <xsl:value-of select="name()"/>
                </th>   
                <xsl:for-each select="$rec">
                    <td>
                        <xsl:value-of select="*[$i]"/>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

</xsl:stylesheet>
  • Related