I am new in XSL and XSLT and I have been trying to get the following sibling of my node but after trying several methods it is still not working.
I am using this glyph/image/following-siblings::image[1]/path
to reference following sibling.
This is how my XML structured.
<?xml version="1.0" encoding="UTF-8"?>
<tablet id="A">
<side id="Aa">
<line id="Aa01">
<glyph id="Aa01-001">
<image id="Aa01-001-b" type="b">
<path id="path-1" d=""/>
<x>7.6107272</x>
<y>21.662689</y>
<width>52.389273</width>
<height>55.234216</height>
</image>
<image id="Aa01-001-f" type="f">
<path id="path-2" d=""/>
<x>9.9999</x>
<y>25.437278</y>
<width>34.33299</width>
<height>73.835859</height>
</image>
</glyph>
<glyph id="Aa01-002">
<image id="Aa01-002-b" type="b">
<path id="path-3" d=""/>
<x>58.25</x>
<y>21.41996</y>
<width>22.113771</width>
<height>75.019102</height>
</image>
<image id="Aa01-002-f" type="f">
<path id="path-4" d=""/>
<x>63.205795</x>
<y>24.00143</y>
<width>22.582803</width>
<height>74.659189</height>
</image>
</glyph>
</line>
</side>
</tablet>
And my XSLT looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet PUBLIC "Unofficial XSLT 1.0 DTD" "http://www.w3.org/1999/11/xslt10.dtd">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>
<!-- Main template -->
<xsl:template match="/">
<div>
<xsl:apply-templates select="tablet"/>
</div>
</xsl:template>
<!-- Tablet -->
<xsl:template match="tablet">
<xsl:apply-templates select="side"/>
</xsl:template>
<!-- Side -->
<xsl:template match="side">
<h2>
<xsl:copy-of select="@id"/>
<xsl:value-of select="side-name"/>
</h2>
<xsl:apply-templates select="line"/>
</xsl:template>
<!-- Line -->
<xsl:template match="line">
<xsl:variable name="curLine" select="line-num"/>
<h3>
<xsl:copy-of select="@id"/>
<xsl:text>Line </xsl:text>
<xsl:value-of select="line-num"/>
</h3>
<xsl:call-template name="drawGlyphLine"/>
</xsl:template>
<!-- Glyphs-->
<xsl:template name="drawGlyphLine">
<p>
<xsl:text>Glyphs: </xsl:text>
<xsl:value-of select="count(glyph/image[@type=$display-type])"/>
</p>
<div>
<xsl:attribute name="height">
<xsl:value-of select="160"/>
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="svg-width[@type=$display-type]"/>
</xsl:attribute>
<xsl:apply-templates select="glyph/image[@type=$display-type]"/>
</div>
</xsl:template>
<!-- Glyph image -->
<xsl:template match="glyph/image">
<svg>
<g>
<xsl:copy-of select="glyph/image/following-siblings::image[1]/path"> <!--Here I want previous sibling of path-->
</xsl:copy-of>
</g>
<g>
<xsl:copy-of select="path"> <!-- This line is making the output a copy of path-->
</xsl:copy-of>
</g>
<g>
<xsl:copy-of select="glyph/image/preceding-sibling::image[1]/path"> <!--Here I want next sibling of path-->
</xsl:copy-of>
</g>
</svg>
</xsl:template>
</xsl:stylesheet>
In the output, I want the following sibling of a current path. I have not added the full code cause of not making it unnecessarily long. Basically, full code automatically makes a copy of the path and renders the SVG. Now I just want the next and previous sibling.
Now the output should be like this.
<svg>
<!-- previous path node -->
<path id="path-1" d=""/>
<x>7.6107272</x>
<y>21.662689</y>
<width>52.389273</width>
<height>55.234216</height>
<!-- next path node-->
</svg>
<svg>
<!-- previous path node -->
<path id="path-2" d=""/>
<x>7.6107272</x>
<y>21.662689</y>
<width>52.389273</width>
<height>55.234216</height>
<!-- next path node -->
</svg>
<svg>
<!-- previous path node -->
<path id="path-3" d=""/>
<x>7.6107272</x>
<y>21.662689</y>
<width>52.389273</width>
<height>55.234216</height>
<!-- next path node -->
</svg>
<svg>
<!-- previous path node -->
<path id="path-4" d=""/>
<x>7.6107272</x>
<y>21.662689</y>
<width>52.389273</width>
<height>55.234216</height>
<!-- next path node -->
</svg>
<svgg>
<!-- previous path node -->
<path id="path-5" d=""/>
<x>7.6107272</x>
<y>21.662689</y>
<width>52.389273</width>
<height>55.234216</height>
<!-- next path node -->
<svg>
Your example has unique image IDs. However, if you want to traverse forwards and backwards, you can use the preceding
axis. That is, if it is not causing any performance issues.
In your glyph/image
template, store the @id
first in a variable
<xsl:variable name="curr_id" select="@id"/>
then use that as you traverse in either preceding or following axis
preceding::image[@id=$curr_id][1]/path
following::image[@id=$curr_id][1]/path
Your template should look like this:
<xsl:template match="glyph/image">
<xsl:variable name="curr_id" select="@id"/>
<svg>
<g>
<xsl:copy-of select="preceding::image[@id=$curr_id][1]/path"> <!--Here I want previous sibling of path-->
</xsl:copy-of>
</g>
<g>
<xsl:copy-of select="path"> <!-- This line is making the output a copy of path-->
</xsl:copy-of>
</g>
<g>
<xsl:copy-of select="following::image[@id=$curr_id][1]/path"> <!--Here I want next sibling of path-->
</xsl:copy-of>
</g>
</svg>
</xsl:template>
I have modified your example in the following link (http://xsltransform.net/pPgCH6G). That should do it if my assumptions are right.