Note that there are some explanatory texts on larger screens.

plurals
  1. POSlugify an Acronym List with XSLT
    text
    copied!<p>Have a requirement to create slugs from XML content.</p> <p>I was navigating to create Slugs from Titles and found the below ones listed.</p> <ul> <li><a href="https://stackoverflow.com/questions/2993119/creating-slugs-from-titles">Creating Slugs from Titles?</a></li> <li><a href="https://stackoverflow.com/questions/1657193/java-code-library-for-generating-slugs-for-use-in-pretty-urls">Java code/library for generating slugs (for use in pretty URLs)</a></li> <li>and similar others.</li> </ul> <p>I was looking for perfect solution for an XSL solution, which provides better slugifies like a PHP solution: <a href="http://blog.tersmitten.nl/slugify" rel="nofollow noreferrer">http://blog.tersmitten.nl/slugify</a> and also takes care of duplicates in the file system.</p> <h3>Input XML</h3> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt;2D&lt;/td&gt; &lt;td&gt;Two Dimension&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;A/C&lt;/td&gt; &lt;td&gt;Account&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;A/C&lt;/td&gt; &lt;td&gt;Air Condition&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;L&amp;T&lt;/td&gt; &lt;td&gt;Larsen &amp; Toubro&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;M + [ # ]&lt;/td&gt; &lt;td&gt;Modified Algo&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <h3>Expected Output</h3> <pre><code>file: 2d.txt ------------ 2D Two Dimension file: a-c.txt ------------- A/C Account file: a-c-2.txt --------------- A/C Air Condition file: l-t.txt ------------- L&amp;T Larsen &amp; Turbo file: m.txt (NOT m-.txt) ------------- M + [ # ] Modified Algo </code></pre> <h3>Tried XSL</h3> <pre><code>&lt;?xml version="1.0"?&gt; &lt;xsl:stylesheet extension-element-prefixes="redirect" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:redirect="http://xml.apache.org/xalan/redirect"&gt; &lt;xsl:output method="text" version="1.0" /&gt; &lt;xsl:template match="/"&gt; &lt;xsl:for-each select="table/tr"&gt; &lt;!-- The logic for variable value requires attention!! --&gt; &lt;xsl:variable name="acronym" select="translate(td[1], 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')" /&gt; &lt;xsl:variable name="tmpFilename" select="concat('C:/Temp/', $acronym, '.txt')" /&gt; &lt;xsl:variable name="filename"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="document($tmpFilename)"&gt; &lt;!-- Require logic to handle duplicate file existence. --&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="$filename" /&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:variable&gt; &lt;redirect:write select="$filename"&gt; &lt;xsl:value-of select="td[1]" /&gt; &lt;xsl:text&gt;&amp;#10;&lt;/xsl:text&gt; &lt;xsl:value-of select="td[2]" /&gt; &lt;/redirect:write&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>Note:</strong> The file name should include only the OS supported characters. The rest should be converted to '-'. Also, there shouldn't be leading and trailing '-' characters (e.g. the last output file as mentioned above).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload