Skip to content

Latest commit

 

History

History
149 lines (129 loc) · 3.38 KB

README.org

File metadata and controls

149 lines (129 loc) · 3.38 KB

Ob-xslt

Org babel extension to apply xslt templates to xml source blocks or source block results.

Processing a named source block

Example data in source block

#+name: my-xml-file
#+begin_src nxml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
 <cd>
   <title>Empire Burlesque</title>
   <artist>Bob Dylan</artist>
   <country>USA</country>
   <company>Columbia</company>
   <price>10.90</price>
   <year>1985</year>
 </cd>
 <cd>
   <title>Hide your heart</title>
   <artist>Bonnie Tyler</artist>
   <country>UK</country>
   <company>CBS Records</company>
   <price>9.90</price>
   <year>1988</year>
 </cd>
 <cd>
   <title>Greatest Hits</title>
   <artist>Dolly Parton</artist>
   <country>USA</country>
   <company>RCA</company>
   <price>9.90</price>
   <year>1982</year>
 </cd>
</catalog>
#+end_src

Example derived from: https://www.w3schools.com/xml/xsl_intro.asp

XSLT block

The xml source is provided via the input variable in the source block header.

#+begin_src xslt :var input=(extract-src-content "my-xml-file")
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
 <html>
 <body>
 <h2>My CD Collection</h2>
 <table border="1">
   <tr bgcolor="#9acd32">
     <th>Title</th>
     <th>Artist</th>
   </tr>
   <xsl:for-each select="catalog/cd">
   <tr>
     <td><xsl:value-of select="title"/></td>
     <td><xsl:value-of select="artist"/></td>
   </tr>
   </xsl:for-each>
 </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>
#+end_src

Example derived from: https://www.w3schools.com/xml/xsl_intro.asp

You can provide a named source block as input using the following elisp snippet.

(defun extract-src-content (name)
  (save-excursion
    (org-babel-goto-named-src-block name)
    (org-element-property :value (org-element-at-point))))

Source: https://emacs.stackexchange.com/questions/29131/get-contents-of-a-named-source-block

Results

<html><body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>Empire Burlesque</td>
<td>Bob Dylan</td>
</tr>
<tr>
<td>Hide your heart</td>
<td>Bonnie Tyler</td>
</tr>
<tr>
<td>Greatest Hits</td>
<td>Dolly Parton</td>
</tr>
</table>
</body></html>

Processing source block output

Alternatively you can consume the output of, for example, an api call from a restclient source block (or any other language block that yields xml ouput) by providing the source block name (with brackets and relevant parameters) as the input variable.

#+name: my-api
#+BEGIN_SRC restclient :var endpoint="localhost:8080"
GET :endpoint/api?foo=bar
#+END_SRC
#+begin_src xslt :var input=my-api()
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
 <html>
 <body>
 <h2>My CD Collection</h2>
 <table border="1">
   <tr bgcolor="#9acd32">
     <th>Title</th>
     <th>Artist</th>
   </tr>
   <xsl:for-each select="catalog/cd">
   <tr>
     <td><xsl:value-of select="title"/></td>
     <td><xsl:value-of select="artist"/></td>
   </tr>
   </xsl:for-each>
 </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>
#+end_src