Skip to content

Instantly share code, notes, and snippets.

@Flackus
Created March 1, 2011 15:11
Show Gist options
  • Save Flackus/849258 to your computer and use it in GitHub Desktop.
Save Flackus/849258 to your computer and use it in GitHub Desktop.
Challenge 2.5
<result>
<r>
<position>3</position>
<content>
<div class="b">
<div class="c">
<b>...</b>
<!-- мы тут -->
</div>
</div>
</content>
</r>
<r>
<position>1</position>
<content></content>
</r>
<r>
<position>4</position>
<content>
<i class="b">
<div class="GR">
<span></span>
</div>
<div class="b">
<div class="c">
<c>
<!-- мы тут -->
<test>
<ololo>...</ololo>
</test>
</c>
</div>
<div class="GR">
<span></span>
</div>
</div>
</i>
</ololo>
</div>
</div>
<div>
<div class="a">
<i>
<div class="b">
<div class="c">
<b>...</b>
<!-- мы тут -->
</div>
</div>
</i>
</div>
<div class="a">
<div>
<d>...</d>
<!-- мы тут -->
</div>
</div>
<div>
<ololo>
<i class="b">
<div class="GR">
<span></span>
</div>
<div class="b">
<div class="c">
<c>
<!-- мы тут -->
<test>
<ololo>...</ololo>
</test>
</c>
</div>
<div class="GR">
<span></span>
</div>
</div>
</i>
</ololo>
</div>
</div>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<result>
<xsl:apply-templates/>
</result>
</xsl:template>
<xsl:template match="b | c | d">
<r>
<xsl:variable name="index">
<xsl:apply-templates select="ancestor::*[not(@class)][1]" mode="count">
<xsl:with-param name="pId" select="generate-id()"/>
</xsl:apply-templates>
</xsl:variable>
<position>
<xsl:value-of select="$index"/>
</position>
<content>
<xsl:copy-of select="ancestor::*[position() &lt; number($index)][last()]"/>
</content>
</r>
</xsl:template>
<xsl:template match="*" mode="count">
<xsl:param name="pId"/>
<xsl:value-of select="
count(
descendant::*
[descendant-or-self::*[generate-id() = $pId]]
[not(ancestor::*[generate-id() = $pId])]
)
"/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment