0

EDIT:
I think I found a way around this issue while still using XSLT 1.0, although very verbose and probably prone to errors.


Is there a way to use the <xsl:number> element but start counting at zero instead of one, especially when using level="multiple"?

I'm using XSLT 1.0.

Current result: 1.2.2. Subitem_B
Expected result: 0.1.1. Subitem_B

I tried format="0" but it only outputs the points (i.e. ... Subitem_B)

<xsl:number level="multiple" format="0. "/>

Bonus question: I found a way around it using translate($number,'.','-'), but I'd still be curious to know if there is a way use a custom character instead of the points as delimiters, or even a custom format altogether.

XML Input

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl" version="1.0"?>
<root>
  <item>Main_A
    <item>Item_A</item>
    <item>Item_B
      <item>Subitem_A</item>
      <item>Subitem_B</item>
    </item>
    <item>Item_C</item>
  </item>
  <item>Main_B
    <item>Item_A
      <item>Subitem_A</item>
      </item>
    <item>Item_B</item>
  </item>
</root>

XSLT 1.0 Stylesheet

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="root">
  <html>
    <body>
      <xsl:apply-templates match="item"/>
    </body>
  </html>
</xsl:template>

<xsl:template match="item">
  <xsl:number level="multiple" format="1. "/>
  <xsl:apply-templates match="item"/>
</xsl:template>

</xsl:stylesheet>

HTML Output

<html>
  <body>
  1. Main_A
    1.1. Item_A
    1.2. Item_B
      1.2.1. Subitem_A
      1.2.2. Subitem_B

    1.3. Item_C

  2. Main_B
    2.1. Item_A
      2.1.1. Subitem_A

    2.2. Item_B

</body>
</html>
Community
  • 1
  • 1
rmercier
  • 155
  • 1
  • 10
  • Which XSLT 1.0 processor are you using? – michael.hor257k May 05 '17 at 12:37
  • @michael.hor257k I'm using [lxml](http://lxml.de/) with a small Python script very similar to [this one](http://stackoverflow.com/questions/16698935/how-to-transform-an-xml-file-using-xslt-in-python/16699042#16699042) – rmercier May 05 '17 at 13:01

2 Answers2

1

I think you would need to move to XSLT 3.0 and use https://www.w3.org/TR/xslt-30/#start-at e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    exclude-result-prefixes="xs math"
    version="3.0">

    <xsl:template match="root">
        <html>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="item">
        <xsl:number level="multiple" format="1. " start-at="0"/>
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

with oXygen 19 and Saxon 9.7 PE gives the result

<html>
   <body>
        0. Main_A
            0.0. Item_A
            0.1. Item_B
                0.1.0. Subitem_A
                0.1.1. Subitem_B

            0.2. Item_C

        1. Main_B
            1.0. Item_A
                1.0.0. Subitem_A

            1.1. Item_B


   </body>
</html>

for your input sample.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks. That's what I feared. Do you know if there is a way to process XSLT 3.0 stylesheets with the lxml Python module? ([this is how I currently use it](http://stackoverflow.com/a/16699042)) ; EDIT: The answer is no, unfortunately. I'm stuck with 1.0 :( [lxml documentation](http://lxml.de/xpathxslt.html) – rmercier May 05 '17 at 11:57
1

If your processor supports the EXSLT str:tokenize() extension function (as I believe it does), you can do:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">

<xsl:template match="/root">
    <html>
        <body>
            <ul>
                <xsl:apply-templates select="item"/>
            </ul>
        </body>
    </html>
</xsl:template>

<xsl:template match="item">
    <xsl:variable name="num">
        <xsl:number level="multiple" format="1."/>
    </xsl:variable>
    <li>
        <xsl:text>path:</xsl:text>
        <xsl:for-each select="str:tokenize($num, '.')">         
            <xsl:value-of select=". - 1" />
            <xsl:if test="position()!=last()">
                <xsl:text>:</xsl:text>
            </xsl:if>
        </xsl:for-each>
        <xsl:text> = </xsl:text>
        <xsl:value-of select="text()" />
        <xsl:if test="item">
            <ul>
                <xsl:apply-templates select="item"/>
            </ul>
        </xsl:if>
    </li>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, the result will be:

<html>
<body>
<ul>
<li>path:0 = Main_A
    <ul>
<li>path:0:0 = Item_A</li>
<li>path:0:1 = Item_B
      <ul>
<li>path:0:1:0 = Subitem_A</li>
<li>path:0:1:1 = Subitem_B</li>
</ul>
</li>
<li>path:0:2 = Item_C</li>
</ul>
</li>
<li>path:1 = Main_B
    <ul>
<li>path:1:0 = Item_A
      <ul>
<li>path:1:0:0 = Subitem_A</li>
</ul>
</li>
<li>path:1:1 = Item_B</li>
</ul>
</li>
</ul>
</body>
</html>

rendered as:

enter image description here

which I believe is the result you asked for in your other question.


Note that match is not a valid attribute of xsl:apply-templates.

Community
  • 1
  • 1
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Wow, thanks, this is perfect. I'm even a bit surprised that I didn't have to install EXSLT or import it into my Python script or anything. So I'm still not sure how exactly it's imported but it works! *"Note that `match` is not a valid attribute of `xsl:apply-templates`."* => Yes this is because I still struggle with the default templates issue and wanted to keep the snippet short. I tried using `select="."` but it gave me garbage data from the children of the nodes I was interested in. Apparently `select="text()"` is the way to avoid that. Glad to have found that out too. – rmercier May 05 '17 at 14:26