Search

Is there a way to get the name of the current (or any other) node?

The functions name() and local-name return item when called and the function node-name() is not supported in XSLT 1.0.

I have this XML (see attachement) and I want to recursively loop from

//page[@handle='stiri']//page[@handle='test']

to the first /<page/> node from the set.

I think the only way would be to add an attribute to every node with the node's name:

<page node-name="page" ... >
    ...
</page>

This is the template responsible for recursion:

<xsl:template name="urlPageParents">
    <xsl:param name="nodePage" />
    <xsl:param name="countryCode" />

    <xsl:if test="name() == 'page'">
        <xsl:call-template name="urlPageParents">
            <xsl:with-param name="nodePage" select=".." />
            <xsl:with-param name="countryCode" select="$countryCode" />
            <xsl:with-param name="i" select="$i - 1" />
        </xsl:call-template>
    </xsl:if>

    <xsl:variable name="countryHandle" select="concat('lhandle-',$countryCode)" />

    <xsl:value-of select="starts-with(name(), $countryHandle)" />

Attachments:
navigare.xml

You're doing recursions with named templates. That makes things more complicated... What exactly is it you're trying to do?

I have this XML (see attachement) and I want to recursively loop from

//page[@handle='stiri']//page[@handle='test']

to the first /<page/> node from the set.

Well I know the starting node ( @handle='stiri' ) so I don't really need the first node name. But the question remains.

What exactly is it you're trying to do?

Generate a translated version of the current page:

This URL from english

domain.com/en/en-page1/en-page2/en-page3/url-p1/url-p2

has to be translated to romanian

domain.com/ro/ro-page1/ro-page2/ro-page3/url-p1/url-p2

You can see from the XML that I have all the necessary strings.

I'm using this template to call himself and after that print the necessary page handles on the link :)

Matching templates are much more suited for recursion. You can recursively parse all page elements with an attribute handle set to test elements and generate a URL out of all lhandle-*-attributes using

<xsl:template match="page">
   <xsl:text><xsl:value-of select="*[@name() == concat("lhandle-", $lang)]" />/<xsl:text>
   <xsl:apply-templates select="//page[@handle='test'][0]" />
</xsl:template>

Start the first iteration by using

<xsl:variable name="lang" select="'ro'" />
<xsl:apply-templates select="//page[@handle='stiri']" />

One word: elegant.

Let me try this. Brb.

I've changed the above XSLT to suit the usecase you're describing in comment #4

I have to go now. It's urgent. But I'll check it out.

One problem that I noticed from-the-fly.

In my XML there is a 3-level depth from page[@hadle="stiri"] to page[@hadle="test"].

Using <xsl:apply-templates select="//page[@handle='test'][0]" /> will jump over page[@hadle="titlu"]. I need to process every page child from start node to end node.

Start node : page[@hadle="stiri"]

End node: page[@hadle="test"]

Child(s) from ascending line: page[@hadle="titlu"].

Thanks for the start anyway.

Edit Having this situation, I thought about going from bottom to top. From "end" to "start". This way I know for certain the ascending line.

Oh I think I misunderstood you somewhere. Without testing, this should be your XSLT now:

<xsl:template match="page">
   <xsl:text><xsl:value-of select="*[@name() == concat("lhandle-", $lang)]" />/<xsl:text>
   <xsl:apply-templates select="page[descendant::page[@handle='test']]" />
</xsl:template>

Line 3 matches all page elements that have a descendant (an element somewhere in them, even if it's nested within other elements) with @handle='test'

Good idea with descendant::. I didn't know it exists. I made some syntax corrections and xPath correction but the main idea is good.

<xsl:value-of select="*[name() = concat('lhandle-', $codTara)]" />

Careful with this: xsl:value-of.../> can't be included inside <xsl:text> </xsl:text>.

Is it still the case that accessing the node name() is tricky in XSLT 1.0?

I'm trying to retrieve the node name of <mobile nandle-"0987070.... to use in a template along with a few others and can't seem to access the name only the value.

Is there a simple syntax for doing such a match?

Got it.. local-name() seems to work.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details