Indent XSL / XPath

What conventions (if any) do you use to indent XSL code?

  • How do you feel about really long complex XPaths.
  • Can you plug them into your XML editor?
  • Is there some kind of open source code that does this job well?

For some background, I use nxml mode in Emacs. For the most part, this is OK, and you can adjust the number of spaces in which children should be indented. This is not very good, although when it comes to complex XPath. If I have a long XPath in my code, I like to make it structure as transparent as possible, making it look like this ...

<xsl:for-each select="/some /very[@test = 'whatever'] /long[@another-test = perhaps /another /long /xpath[@goes='here']] /xpath" 

However, for now, I have to do this manually, since nxml will just align everything with "/ some .."

+6
xpath xslt
source share
4 answers

Sometimes a longer xpath cannot be avoided even if you use templates instead of for-eaches (for example, if you want). This is especially true in XSLT / XPath 2.0:

 <xsl:attribute name="tablevel" select="if (following::*[self::topic | self::part]) then (following::*[self::topic | self::part])[1]/@tablevel else @tablevel"/> 

I try not to break the "simple" path into lines, but to break the "larger" path into operators or conventions.

For editing, I use Oxygen (which is cross-platform), and it does pretty well with this type of distance. Sometimes it does not predict what you want for sure, but it will maintain the space as soon as it is there, even if you re-release your code.

+1
source share

In my opinion, long xpaths are hard to read and should be avoided. There are two ways to do this:

  • Simplify the source xml.
  • Divide large templates into smaller ones.
+1
source share

I tend to decompose XSL in different ways if I find it difficult to read xpath instructions (which doesn't happen often, but sometimes it happens) ... actually it looks like my methods of splitting syntax into other languages ​​... So, your example in the question may become something more like this:

 <xsl:for-each select="/some/very[@test = 'whatever']/long"> <xsl:if test="@another-test = perhaps/another/long/xpath[@goes='here']"> <xsl:for-each select="xpath"> ... result xml .... </xsl:for-each> </xsl:if> </xsl:for-each> 
+1
source share

Do not use long xpaths. Align for each and use matching patterns. Break xpath into several patterns. It’s much easier to read a bunch of trivial match patterns than one of them.

+1
source share

All Articles