I am trying to learn xslt and i am working an xml which looks like this.
<Beats>
<Beat>
<Personal type="set">
<Usages type="box">
1233
</Usages>
<NonUsages type="box">
4122
</NonUsages>
</Personal>
<NonPersonal type="unset">
<Damages type="box">
6466
</Damages>
<NonDamages type="box">
5544
</NonUsages>
</NonPersonal>
<Confidential type="set">
<Discounts type="box">
1233
</Discounts>
<NonDiscounts type="box">
4122
</NonDiscounts>
</Confidential>
</Beat>
</Beats>
My Current aim is to just print out the numbers inside the inner tags. But i cannot use the names of the tags as selectors as only the attribute ‘type’is of importance. I tried using the following xslt. But it didnt seem to work.
<xsl:output method="text"/>
<xsl:template match="*">
<html>
<body>
<h2> Test</h2>
<xsl:for-each select="//Beats/Beat/[@type='set']">
<xsl:value-of select="[@type='box'] />
<br/>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
What am i doing wrong ?
And one more thing i couldnt figure out was how to get the name of tags while using attributes as selectors. Instead of
<xsl:value-of select="[@type='box'] />
which gives whats inside the tag; what can i use to get the name of the tag which contains this ‘type=box’ attribute ? (For. eg Usages, NonUsages, etc..)
This transformation is intended to only provide the necessary corrections to your original transformation:
When this transformation is applied on the provided XML document (after correcting it to make it well-formed XML document):
the (what I guess is) wanted result is produced:
and it is displayed in the browser as:
Test
1233
4122
1233
4122
On your second question:
You want something as:
The complete transformation now becomes:
and when it is applied on the same XML document (above), the result is:
and this is displayed by the browser as:
Test
Usages :
1233
NonUsages :
4122
Discounts :
1233
NonDiscounts :
4122