Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tag name/attribute name in XML using XSLT

Tags:

xml

tags

xslt

What's the way to get a tag name and attribute name in XML?

If I have a XML file like this:

<a>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</a>

And part of my XSLT file is as below:

<xsl:element name="AAA">
<???>
</xsl:element>

So what should I write in the ??? part so I can get the output like this:

For tag name:

<AAA>apple</AAA>
<AAA>banana</AAA>
<AAA>sugar</AAA>
<AAA>cat</AAA>

For attribute name:

<AAA>color</AAA>
<AAA>color</AAA>
<AAA>taste</AAA>
<AAA>size</AAA>
like image 492
ahliang1412 Avatar asked Dec 15 '11 15:12

ahliang1412


People also ask

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit)

What is param name in XSLT?

XSLT <xsl:param> The <xsl:param> element is used to declare a local or global parameter. Note: The parameter is global if it's declared as a top-level element, and local if it's declared within a template.


3 Answers

Tag name:

<xsl:value-of select="name(.)"/>

Attribute name of the first (!) attribute. If you have more attributes, you'd have to choose a different approach

<xsl:value-of select="name(@*[1])"/>

Both expressions would then be used in a template matching your input elements. e.g.

<xsl:template match="*">
  <xsl:element name="AAA">
    <!-- ... -->
  </xsl:element>
</xsl:template>
like image 60
Lukas Eder Avatar answered Sep 20 '22 15:09

Lukas Eder


Output the name of an element or attribute using one of name() or local-name():

<xsl:value-of select="name()"/>
<xsl:value-of select="local-name()"/>

Assume this document:

<root>
    <apple color="red"/>
    <banana color="yellow"/>
    <sugar taste="sweet"/>
    <cat size="small"/>
</root>

Then this stylesheet:

<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="/">
        <root>
            <xsl:apply-templates select="/*/*"/>
            <xsl:apply-templates select="/*/*/@*"/>
        </root>
    </xsl:template>
    <xsl:template match="*|@*">
        <AAA><xsl:value-of select="local-name()"/></AAA>
    </xsl:template>
</xsl:stylesheet>

Produces:

<root>
   <AAA>apple</AAA>
   <AAA>banana</AAA>
   <AAA>sugar</AAA>
   <AAA>cat</AAA>
   <AAA>color</AAA>
   <AAA>color</AAA>
   <AAA>taste</AAA>
   <AAA>size</AAA>
</root>

Notice that both elements and attributes are handled by the same template.

like image 40
Wayne Avatar answered Sep 20 '22 15:09

Wayne


This is probably one of the shortest solutions:

<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:template match="*/*|@*">
  <AAA>
   <xsl:value-of select="name()"/>
  </AAA>
   <xsl:apply-templates select="@*"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document (your fragment wrapped into a top element):

<things>
    <apple color="red"/>
    <banana color="yellow"/>
    <sugar taste="sweet"/>
    <cat size="small"/>
</things>

the wanted, correct result is produced:

<AAA>apple</AAA>
<AAA>color</AAA>
<AAA>banana</AAA>
<AAA>color</AAA>
<AAA>sugar</AAA>
<AAA>taste</AAA>
<AAA>cat</AAA>
<AAA>size</AAA>
like image 35
Dimitre Novatchev Avatar answered Sep 18 '22 15:09

Dimitre Novatchev