Xml - XSL Transformations (XSLT)
About
XSLT is the most important part of XSL.
Extensible Stylesheet Language Transformation (XSLT):
- is a language for transforming XML documents into other XML documents (and particularly into (X)HTML documents).
- specify transformations on the data in order to convert it
- uses XPath (mechanisms for addressing XML data) to navigate in XML documents
See template for processing
Example
With the xsltproc xml command line utility
- Xml Source File:
curl -s https://whois.arin.net/rest/poc/KOSTE-ARIN | xq
<?xml version='1.0'?>
<?xml-stylesheet type='text/xsl' href='https://whois.arin.net/xsl/website.xsl' xmlns="http://www.arin.net/whoisrws/core/v1"?>
<poc>
<registrationDate>2009-10-02T11:54:45-04:00</registrationDate>
<rdapRef>https://rdap.arin.net/registry/entity/KOSTE-ARIN</rdapRef>
<ref>https://whois.arin.net/rest/poc/KOSTE-ARIN</ref>
<city>Chantilly</city>
<companyName>ARIN</companyName>
<iso3166-1>
<code2>US</code2>
<code3>USA</code3>
<name>United States</name>
<e164>1</e164>
</iso3166-1>
<firstName>Mark</firstName>
<handle>KOSTE-ARIN</handle>
<isRoleAccount>N</isRoleAccount>
<lastName>Kosters</lastName>
<emails>
<email>[email protected]</email>
<email>[email protected]</email>
</emails>
<resources copyrightNotice="Copyright 1997-2025, American Registry for Internet Numbers, Ltd." inaccuracyReportUrl="https://www.arin.net/resources/registry/whois/inaccuracy_reporting/" termsOfUse="https://www.arin.net/resources/registry/whois/tou/">
<limitExceeded limit="256">false</limitExceeded>
</resources>
<phones>
<phone>
<number>+1-703-227-9870</number>
<type>
<description>Office</description>
<code>O</code>
</type>
</phone>
</phones>
<pocType>
<description>General use</description>
<type>G</type>
</pocType>
<postalCode>20151</postalCode>
<comment>
<line number="0">I'm really MAK21-ARIN</line>
</comment>
<iso3166-2>VA</iso3166-2>
<status>
<description>Unverified</description>
<code>PU</code>
</status>
<streetAddress>
<line number="0">3635 Concorde Parkway</line>
</streetAddress>
<updateDate>2024-09-09T13:16:31-04:00</updateDate>
</poc>
- xsl. The whois namespace is mandatory as it's defined as default namespace in the data and an empty namespace is not allowed in xpath, otherwise you get no selection and xsl output just the text content
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:whois="http://www.arin.net/whoisrws/core/v1">
<xsl:output method="text"/>
<xsl:template match="whois:poc">
Ref:
<xsl:value-of select="whois:ref"/>
Name: <xsl:value-of select="whois:lastName"/>,
<xsl:value-of select="whois:firstName"/>
<xsl:value-of select="whois:middleName"/>
Handle:
<xsl:value-of select="whois:handle"/>
Company:
<xsl:value-of select="whois:companyName"/>
Address:
<xsl:value-of select="whois:streetAddress"/>
City:
<xsl:value-of select="whois:city"/>
StateProv:
<xsl:value-of select="whois:iso3166-2"/>
Country:
<xsl:value-of select="whois:iso3166-1/code2"/>
RegDate:
<xsl:value-of select="whois:registrationDate"/>
<xsl:text>sampletext</xsl:text>
</xsl:template>
</xsl:stylesheet>
- Command (fetches an xhtml page)
xsltproc poc.xsl http://whois.arin.net/rest/poc/KOSTE-ARIN
# not https otherwise you get an error: failed to load external entity, unable to parse
- Output:
Ref: http://whois.arin.net/rest/poc/KOSTE-ARIN
Name: Kosters, Mark
Handle: KOSTE-ARIN
Company: ARIN
Address: PO Box 232290
City: Centreville
StateProv: VA
Country: US
RegDate: 2009-10-02T11:54:45-04:00