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.
<?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.
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
<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.
这篇关于如何使用子节点的 xslt 从 xml 获取以下兄弟节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!