XSLT Parameters: Bridging the Gap Between Relational Databases and XML Filtering
In relational databases, filtering data from tables is a straightforward process, often enhanced by the use of parameterized stored…
XSLT Parameters: Bridging the Gap Between Relational Databases and XML Filtering
In relational databases, filtering data from tables is a straightforward process, often enhanced by the use of parameterized stored procedures. In the XML realm, a similar functionality can be achieved through the use of parameters in XSLT, offering flexible and dynamic data transformations.
In this article, we’ll dive deep into transforming XML using XSLT with the Saxon processor as our tool of choice.
Photo by Shahadat Rahman on Unsplash
Why Saxon?
While there are numerous XSLT processors available, Saxon stands out for a few reasons:
- It supports the latest versions of XSLT.
- It is reliable, efficient, and widely adopted in the industry.
- Saxon offers a free version, Saxon-HE (Home Edition), which is open-source.
Step 1: Setting Up
1.1. Download and Install Saxon-HE:
Visit the official Saxon website and download Saxon-HE. Once downloaded, unzip the archive.
1.2. Prepare XML Data:
For our example, we’ll use the following XML representing a list of employees:
xmlCopy code
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<name>John Doe</name>
<department>HR</department>
<salary>10000</salary>
</employee>
<employee>
<name>Jane Smith</name>
<department>Finance</department>
<salary>12000</salary>
</employee>
...
</employees>
Step 2: Writing the XSLT
Our goal is to filter out employees whose salary exceeds a particular threshold. The XSLT will be designed to accept a parameter for this salary threshold.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Global parameter for salary filtering -->
<xsl:param name="salaryThreshold"/>
<!-- Variable to store employees with salaries greater than the threshold -->
<xsl:variable name="filteredEmployees" select="/employees/employee[salary > $salaryThreshold]"/>
<!-- Main template - entry point of the XSLT -->
<xsl:template match="/">
<result>
<!-- Call the output template to render the filtered employees -->
<xsl:call-template name="outputEmployees"/>
</result>
</xsl:template>
<!-- Template for rendering/outputting filtered employees -->
<xsl:template name="outputEmployees">
<xsl:for-each select="$filteredEmployees">
<employee>
<name><xsl:value-of select="name"/></name>
<department><xsl:value-of select="department"/></department>
<salary><xsl:value-of select="salary"/></salary>
</employee>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Step 3: Transforming XML using Saxon
With our XML and XSLT ready, we can now use the Saxon processor to transform our data.
Navigate to the directory containing the Saxon JAR file in your terminal or command prompt. Execute the following command:
java -jar saxon-he-x.y.z.jar -s:source.xml -xsl:stylesheet.xslt -o:output.xml salaryThreshold=11000
This command instructs the Saxon processor to:
- Take
source.xmlas the source XML file. - Use
stylesheet.xsltas the XSLT file. - Output the results to
output.xml. - Set the
salaryThresholdparameter to11000.
Step 4: Viewing the Results
Open output.xml in your favorite XML or text editor. You should see a transformed XML containing only employees with salaries greater than 11,000.
Conclusion
XSLT is a powerful tool for XML transformations, and the Saxon processor is an excellent choice for these operations due to its robustness and support for the latest XSLT versions. Whether you’re reshaping data for a new application or generating reports, mastering XSLT and tools like Saxon can significantly enhance your XML processing capabilities.
메타데이터
- post_id
- 2daab9f71e55
- slug
- xslt-parameters-bridging-the-gap-between-relational-databases-and-xml-filtering-2daab9f71e55
- url
- https://medium.com/@kishorev1/xslt-parameters-bridging-the-gap-between-relational-databases-and-xml-filtering-2daab9f71e55
- canonical_url
- https://medium.com/@kishorev1/xslt-parameters-bridging-the-gap-between-relational-databases-and-xml-filtering-2daab9f71e55
- author_url
- https://medium.com/@kishorev1
- status
- ok
- fetched_at
- 2026-06-24 11:06:28