5

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>
user673869
  • 221
  • 1
  • 3
  • 10

2 Answers2

8

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(.), ' ')
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Dimitre - is it possible to create a variable out of the attribute name instead of displaying it? I've tried doing but it doesn't work... – user673869 Aug 02 '12 at 16:19
  • @user673869: Yes, this variable contains a sequence of strings, each string being a name of an attribute: `` – Dimitre Novatchev Aug 02 '12 at 16:46
  • Dimitre - I believe your code is written for XSLT 2.0. Could you write an example for 1.0? Also, you gave me this website as a good reference tool for one of my previous questions: http://dpawson.co.uk/xsl/sect2/N5258.html. Is there another that would better explain what I'm trying to accomplish? Thanks! – user673869 Aug 02 '12 at 16:56
  • @user673869: You tagged the question s "xslt-2.0". If this is by mistake, please, delete this tag and tag it as "xslt-1.0" – Dimitre Novatchev Aug 02 '12 at 17:58
  • @user673869: in XSLT 1.0 just put the `xsl:for-each` inside the body of an `xsl:variable`. – Dimitre Novatchev Aug 02 '12 at 18:00
2

What you want is

<xsl:for-each select="candy/@*">
LarsH
  • 27,481
  • 8
  • 94
  • 152
  • cool.. can I return each attribute name (not value) whose value='true'? something like this: ? – user673869 Aug 02 '12 at 01:19
  • @user673869: To select the attribute *nodes* whose value is true, `select="candy/@*[. = 'true']"`. To select or return their *names* is a longer answer ... see Dimitre's answer. – LarsH Aug 02 '12 at 13:26