Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: How can I get the names of all nodes?

Tags:

xslt

I'm looking for a stylesheet which prints the name of each node including its value. Getting the value is easy but I don't know how to get the name of each node. Here's the basic template:

<?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="UTF-8" />   
    <xsl:template match="/">
      <xsl:for-each select="/">
         ???
      </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Could anyone give me a hint?

Thanks, Robert

like image 285
Robert Strauch Avatar asked Dec 28 '22 22:12

Robert Strauch


1 Answers

This complete transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>
 <xsl:variable name="vQ">"</xsl:variable>

 <xsl:template match="*">
     <xsl:value-of select=
       "concat(name(), ' = ', $vQ, ., $vQ, '&#xA;')"/>
    <xsl:apply-templates select="*"/>
 </xsl:template>
</xsl:stylesheet>

when applied on any XML document, such as this:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

produces exactly the wanted, correct result:

nums = "01020304050607080910"
num = "01"
num = "02"
num = "03"
num = "04"
num = "05"
num = "06"
num = "07"
num = "08"
num = "09"
num = "10"
like image 92
Dimitre Novatchev Avatar answered Dec 30 '22 11:12

Dimitre Novatchev