Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use <xsl:for-each> to go through attributes?

Is it possible to use the command on attributes? I want this to be able to run without knowing the attribute names. Here's a quick (terrible) example:

<candy hard="true" soft="false" stripes="true" solid="false">

In my head (this doesn't work) it should look something like this:

<xsl:for-each select="candy/@[@='true']">

Is there a way around this to run through attributes without knowing their name, or do I need to write each attribute being looked at?

Edit

Heres an example of me trying to create a variable out of the attribute name where value='true'

<xsl:for-each select="candy/@*[. = 'true']">
<xsl:attribute name="candytype"> 
   <xsl:value-of select="name()"/> 
</xsl:attribute>
<xsl:text> </xsl:text>
<xsl:for-each>
like image 685
user673869 Avatar asked Jan 15 '23 22:01

user673869


2 Answers

OP's comment:

can I return each attribute name (not value) whose value='true'?

<xsl:for-each select="candy/@*[. = 'true']">
   <xsl:value-of select="name()"/>
   <xsl:text> </xsl:text>
<xsl:for-each>

In XSLT 2.0 simply evaluate this XPath (2.0) expression:

candy/@*[. = 'true']/concat(name(.), ' ')
like image 131
Dimitre Novatchev Avatar answered Jan 22 '23 06:01

Dimitre Novatchev


What you want is

<xsl:for-each select="candy/@*">
like image 22
LarsH Avatar answered Jan 22 '23 08:01

LarsH