Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>XSLT 1.0:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" &gt; &lt;xsl:key name = "kInterfaceByName" match = "interface" use = " substring-before(concat( substring-before(concat(@name, ':'), ':'), '.'), '.') " /&gt; &lt;xsl:template match="i"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates mode="group" select="interface" /&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="interface" mode="group"&gt; &lt;xsl:variable name="name" select=" substring-before(concat( substring-before(concat(@name, ':'), ':'), '.'), '.') " /&gt; &lt;xsl:variable name="interfaces" select="key('kInterfaceByName', $name)" /&gt; &lt;!-- Muenchian step --&gt; &lt;xsl:if test="generate-id()=generate-id($interfaces[1])"&gt; &lt;interface name="{$name}"&gt; &lt;xsl:apply-templates mode="unit" select="$interfaces" /&gt; &lt;/interface&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;xsl:template match="interface" mode="unit"&gt; &lt;unit name="{@name}" /&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>when applied to</p> <pre><code>&lt;i&gt; &lt;interface name="Serial1/0"/&gt; &lt;interface name="Serial2/0.0"/&gt; &lt;interface name="Serial2/0.1"/&gt; &lt;interface name="Serial3/0:0"/&gt; &lt;interface name="Serial3/0:1"/&gt; &lt;/i&gt; </code></pre> <p>results in</p> <pre><code>&lt;i&gt; &lt;interface name="Serial1/0"&gt; &lt;unit name="Serial1/0" /&gt; &lt;/interface&gt; &lt;interface name="Serial2/0"&gt; &lt;unit name="Serial2/0.0" /&gt; &lt;unit name="Serial2/0.1" /&gt; &lt;/interface&gt; &lt;interface name="Serial3/0"&gt; &lt;unit name="Serial3/0:0" /&gt; &lt;unit name="Serial3/0:1" /&gt; &lt;/interface&gt; &lt;/i&gt; </code></pre> <p>The XPath expression replaces your <code>getPhysicalInterfaceName</code> template.</p> <p>It does, on the examples of <code>'Serial2/0.0'</code> and <code>'Serial3/0:1'</code>:</p> <ol> <li>append a <code>':'</code> (=> <code>'Serial2/0.0:'</code>; <code>'Serial3/0:1:'</code>)</li> <li>take everything before the first <code>':'</code> (=> <code>'Serial2/0.0'</code>; <code>'Serial3/0'</code>)</li> <li>append a <code>'.'</code> (=> <code>'Serial2/0.0.'</code>; <code>'Serial3/0.'</code>)</li> <li>take everything before the first <code>'.'</code> (=> <code>'Serial2/0'</code>; <code>'Serial3/0'</code>)</li> </ol> <hr> <p>EDIT: Simplified XPath expression. My first try worked but was more complex:</p> <pre><code>concat( substring-before(@name, '/'), '/', substring-before( concat( translate(substring-after(@name, '/'), '.', ':'), ':' ), ':' ) ) </code></pre> <p>On the plus side, the above expression correctly handles colons and dots in the first part of the name, e.g. <code>'Some.Serial3/0:1'</code>. The shorter one does not. If you expect dots in the name, use the longer expression. An explanation of it is in the <a href="https://stackoverflow.com/posts/2116579/revisions">revision history of this post</a>.</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