Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jump to using XSLT

Tags:

xml

xslt

xpath

I am fetching an XML document from a third party (so I cannot change it) which contains around 1000 records. I am translating this using XSLT to only show the user 50 records at a time. I can control which records are shown by checking the position e.g.

xsl:if test="not(position() < 101)"

xsl:if test="position() < 150"

The user would prefer to jump to a value rather than scrolling through page by page until they find it. The rest of the records should still show after the jump to value so I don't want to check for an exact match. I initially thought that I could do a string comparison e.g.

xsl:if test="@key >= 'jumpto'"

but this is not supported in 1.0. Any ideas how to achieve what I want?

The XML is quite large to post and the data is confidential. But imagine it is a simple XML file with just a name e.g.

<contacts>
    <name>alan</name>
    <name>brad</name>
    <name>chad</name>
    <name>dave</name>
    <name>eric</name>
</contacts>

I want to only show the data starting from dave onwards and I can pass dave to the XSL document.

like image 252
Steve Avatar asked Jun 07 '26 22:06

Steve


1 Answers

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:param name="p">dave</xsl:param>

  <xsl:template match="contacts">
    <out>
      <xsl:apply-templates 
        select="name[. = $p] | name[. = $p]/following-sibling::name"/>
    </out>
  </xsl:template>

  <xsl:template match="name">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

Output:

<out>
  <name>dave</name>
  <name>eric</name>
</out>
like image 189
Kirill Polishchuk Avatar answered Jun 10 '26 18:06

Kirill Polishchuk