Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This solution does what you want:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" /&gt; &lt;xsl:template match="/"&gt; &lt;root&gt; &lt;xsl:attribute name="path"&gt; &lt;xsl:apply-templates select="(//@title)[1]" mode="make-path" /&gt; &lt;/xsl:attribute&gt; &lt;/root&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*|@*" mode="make-path"&gt; &lt;xsl:apply-templates select="parent::*" mode="make-path" /&gt; &lt;xsl:text&gt;/&lt;/xsl:text&gt; &lt;xsl:apply-templates select="." mode="make-name" /&gt; &lt;xsl:choose&gt; &lt;xsl:when test="self::section"&gt; &lt;xsl:apply-templates select="@id" mode="make-predicate" /&gt; &lt;/xsl:when&gt; &lt;xsl:when test="self::subPage"&gt; &lt;xsl:apply-templates select="@user" mode="make-predicate" /&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*|@*" mode="make-predicate"&gt; &lt;xsl:text&gt;[&lt;/xsl:text&gt; &lt;xsl:apply-templates select="." mode="make-name" /&gt; &lt;xsl:text&gt; = '&lt;/xsl:text&gt; &lt;xsl:value-of select="." /&gt; &lt;xsl:text&gt;']&lt;/xsl:text&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*" mode="make-name"&gt; &lt;xsl:value-of select="name()" /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*" mode="make-name"&gt; &lt;xsl:text&gt;@&lt;/xsl:text&gt; &lt;xsl:value-of select="name()" /&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When applied to</p> <pre><code>&lt;section id="someId"&gt; &lt;page&gt; &lt;subPage user="UserA" title="test" /&gt; &lt;subPage user="UserB" title="blah" /&gt; &lt;/page&gt; &lt;page&gt; &lt;subPage user="UserC" title="fooh" /&gt; &lt;/page&gt; &lt;/section&gt; </code></pre> <p>you get:</p> <pre><code>&lt;root path="/section[@id = 'someId']/page/subPage[@user = 'UserA']/@title" /&gt; </code></pre> <p>The <code>&lt;xsl:choose&gt;</code> is the configurable spot (add as many <code>&lt;xsl:when&gt;</code>s as you like):</p> <pre><code>&lt;!-- test for element name --&gt; &lt;xsl:when test="self::section"&gt; &lt;!-- make predicates out of selected attributes --&gt; &lt;xsl:apply-templates select="@id" mode="make-predicate" /&gt; &lt;/xsl:when&gt; </code></pre> <p>also possible:</p> <pre><code>&lt;xsl:when test="self::section"&gt; &lt;xsl:apply-templates select="@name|@category|subElement" mode="make-predicate" /&gt; &lt;/xsl:when&gt; </code></pre> <p>which would lead to</p> <pre><code>&lt;root path="/section[@name = 'someName'][@category = 'somecat'][subElement = 'xyz']/..." /&gt; </code></pre> <p>The only issue I see is with predicate values that contain single quotes. They would break the XPath.</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