Erik Åstrand
Erik Åstrand

Reputation: 379

xsl - Get all attributes from childs

I'm having some trouble getting all the attributes from the parent tag, and their childs. This is my the XML:

<macro name="editor">
        <names variable="editor" delimiter=", ">
            <name and="symbol" delimiter=", "/>
            <label form="short" prefix=" (" text-case="lowercase" suffix=".)" />
       </names>
</macro>

I want to be able to get the and all the attributes from the childnodes. I currently have:

<xsl:for-each select="macro">
   <xsl:value-of select="@*" />
   <br />
</xsl:for-each>

How I want it to turn out:

editor

names editor,

name symbol,

label short ( lowercase .)

Upvotes: 0

Views: 4152

Answers (3)

siva2012
siva2012

Reputation: 459

When this XSLT transformation

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="macro">
  <xsl:value-of select="@name"/>
  <xsl:for-each select="child::*">
    <xsl:text disable-output-escaping="yes">&#10;</xsl:text>
    <xsl:value-of select="name(.)"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="@*" separator=""/>

    <xsl:for-each select="child::*">
      <xsl:text disable-output-escaping="yes">&#10;</xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="@*" separator=""/>
    </xsl:for-each>

  </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

runs on below XML:

<macro name="editor">
        <names variable="editor" delimiter=", ">
            <name and="symbol" delimiter=", "/>
            <label form="short" prefix=" (" text-case="lowercase" suffix=".)" />
       </names>
</macro>

gives the required output:

editor
names editor, 
name symbol, 
label short (lowercase.)

Upvotes: 1

hr_117
hr_117

Reputation: 9627

Try this:

<?xml version="1.0"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*" mode="print_attr">
        <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="node()" mode="print_attr">
        <xsl:text>&#10;</xsl:text>
        <br/>
        <xsl:value-of select="name()"/>
        <xsl:text> </xsl:text>
        <xsl:apply-templates mode="print_attr" select="@*|*" />
    </xsl:template>

    <xsl:template match="macro">
        <xsl:apply-templates mode="print_attr" select="@*|*"/>
    </xsl:template>

</xsl:stylesheet>

Which will generate this output:

editor
<br/>names editor,
<br/>name symbol,
<br/>label short (lowercase.)

Upvotes: 1

Navin Rawat
Navin Rawat

Reputation: 3138

Try this to get all varibale name and value:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output indent="yes"/>
  <xsl:template match="macro">
    <xsl:for-each select="child::*//@*">
      <xsl:value-of select="concat(name(), ' : ', .)"/>
      <xsl:text>
    </xsl:text>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

output:

variable : editor
    delimiter : , 
    and : symbol
    delimiter : , 
    form : short
    prefix :  (
    text-case : lowercase
    suffix : .)

Upvotes: 1

Related Questions