Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display semi-random xml attribute

Status
Not open for further replies.
Feb 7, 2007
7
US
Very similar question as I had a few threads down, but this time, I don't know what the name of the attribute is going to be and would like to display it (the name of the attribute itself, not the value of the attribute)

Example:
XML
<Combine target="A" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N2" CL-1CA39N2="S" />

To Be Displayed:
Target: A, Type: OneToOne, Source Count: 1, Source Count 1: CL-1CA39N2, CL-1CA39N2: S

How do I access the name of the fourth attribute when I do not know what it is going to be?

Again, thank you in advance for any help - it is much appreciated.
 
Umm...that's exactly what I answered in the previous thread.

k5tm previous thread said:
So, if I wanted the name of the fifth attribute on the third <Combine> element in the second <CustomerIndicator> element, i would use an XPath expression something like this. [small]emphasis added[/small]

So, if you are in the context of a <Combine> element, you might try:
Code:
<xsl:for-each select="@*">
    <xsl:value-of slect="local-name(.)"/>
    <xsl:text>:</xsl:text>
    <xsl:value-of slect="."/>
</xsl:for-each]
[small]untested[/small]

Tom Morrison
 
Huh, when I used the information you gave me last time, it gave me the value of the attribute (which is indeed what I was looking for) but not the name of the attribute itself. For instance:

<Combine target="A" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N2" CL-1CA39N2="S" />

In my first question I was looking to get the value of the fifth attribute - in the example above, I was looking to get 'S'. However, now I am trying to determine how to get the name of the fifth attribute (in the example above 'CL-1CA39N2' without knowing what it is going to be named ahead of time.

Am I making sense or am I so hopelessly new at this whole XSL thing that I don't even know what to ask and how to use the answers I am given?

At any rate, thanks for trying to help me even though I may be to ignorant to know when I am being helped!
 
[first, I note that I misspelled 'select' in my previous code example.]

The local-name() function returns the name of the node (in this case the name of the attribute). If you used the entire XPath expression, it should have returned the name, not the valu, of the fifth attribute.

creamcitian previous post said:
Is there a way to display this attribute and its value knowing its place (5th attribute) but not its name?
What I showed in the previous thread was the how to get the name, leaving the value as an exercise.

So here is something to help some more.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
<xsl:output method="text"/>
<xsl:variable name="lf"><xsl:text>
</xsl:text>
</xsl:variable>

<xsl:template match="/">
	<xsl:for-each select="*">
	<xsl:value-of select="local-name(.)"/>:<xsl:value-of select="position()"/><xsl:value-of select="$lf"/>
		<xsl:for-each select="*">
		<xsl:value-of select="concat('  ', local-name(.))"/>:<xsl:value-of select="position()"/><xsl:value-of select="$lf"/>
			<xsl:for-each select="*">
			<xsl:value-of select="concat('    ', local-name(.))"/>:<xsl:value-of select="position()"/><xsl:value-of select="$lf"/>
				<xsl:for-each select="@*">
				<xsl:value-of select="concat('    ', local-name(.))"/>:<xsl:value-of select="position()"/>:<xsl:value-of select="."/><xsl:value-of select="$lf"/>
				</xsl:for-each>
			</xsl:for-each>
		</xsl:for-each>
	</xsl:for-each>
</xsl:template>

Using the input from your previous thread (<root> element added).
XML:
<root>
<CustomIndicator targetCategory="3" targetClassification="1" targetNumber="4">
    <Source category="39" classification="-1" number="2" />
    <Combine target="A" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N2" CL-1CA39N2="S" />
    <Combine target="U" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N2" CL-1CA39N2="U" />
    <Combine target="N" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N2" CL-1CA39N2="N" />
</CustomIndicator>
<CustomIndicator targetCategory="3" targetClassification="1" targetNumber="5">
    <Source category="79" classification="-1" number="3" />
    <Combine target="Y" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA79N3" CL-1CA79N3="Y" />
    <Combine target="M" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA79N3" CL-1CA79N3="X" />
    <Combine target="H" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA79N3" CL-1CA79N3="N" />
    <Combine target="H" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA79N3" CL-1CA79N3="E" />
</CustomIndicator>
<CustomIndicator targetCategory="11" targetClassification="1" targetNumber="2">
    <Source category="39" classification="-1" number="13" />
    <Combine target="D" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N13" CL-1CA39N13="D" />
    <Combine target="E" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N13" CL-1CA39N13="E" />
    <Combine target="N" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N13" CL-1CA39N13="M" />
    <Combine target="O" type="OneToOne" sourceCount="1" sourceCount1="CL-1CA39N13" CL-1CA39N13="N" />
</CustomIndicator>
</root>

Code:
root:1
  CustomIndicator:1
    Source:1
      category:1:39
      classification:2:-1
      number:3:2
    Combine:2
      target:1:A
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N2
      CL-1CA39N2:5:S
    Combine:3
      target:1:U
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N2
      CL-1CA39N2:5:U
    Combine:4
      target:1:N
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N2
      CL-1CA39N2:5:N
  CustomIndicator:2
    Source:1
      category:1:79
      classification:2:-1
      number:3:3
    Combine:2
      target:1:Y
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA79N3
      CL-1CA79N3:5:Y
    Combine:3
      target:1:M
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA79N3
      CL-1CA79N3:5:X
    Combine:4
      target:1:H
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA79N3
      CL-1CA79N3:5:N
    Combine:5
      target:1:H
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA79N3
      CL-1CA79N3:5:E
  CustomIndicator:3
    Source:1
      category:1:39
      classification:2:-1
      number:3:13
    Combine:2
      target:1:D
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N13
      CL-1CA39N13:5:D
    Combine:3
      target:1:E
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N13
      CL-1CA39N13:5:E
    Combine:4
      target:1:N
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N13
      CL-1CA39N13:5:M
    Combine:5
      target:1:O
      type:2:OneToOne
      sourceCount:3:1
      sourceCount1:4:CL-1CA39N13
      CL-1CA39N13:5:N

Tom Morrison
 
Woo! There is a lot of stuff going on there, I will make a day of studying what is going on.

Tom, you are a scholar and a gentleman. Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top