Jump to content

How to grab attributes of XML Data


General_HQ

Recommended Posts

I have what I am sure is a simple, stupid question, but I have spent longer than I should have trying to find the answer, so I thought I'd come here.I have XML data that looks like this:<img id="L001.002.001.P005-001" src="./images/p005-001.gif" alt="Black and white photo of a man during the Depression" /> I have to write some XSL that will recognize that line, ignore the id and src values, and simply grab the alt text, if any. What I tried was:<xsl:template match="//img"><xsl:if test= "@alt != ''">\par <xsl:value-of select="@alt"/> \par </xsl:if></xsl:template>Hoping to produce output that read:\par Black and white photo of a man during the Depression \parbut nothing seems to be coming out. Any ideas what I could be doing wrong?

Link to comment
Share on other sites

Strange. I tryed that and it worked. Something other odd happened though. I added this line to your old XML with the table I still keep on my XSLT bag'o'tricks. Then after applying a stylesheet to it that contained ONLY the template here, I saw the desired output + your table linearized :) .[edit] Fixed it, by using "for-each" instead of a template match. Like this:

<xsl:template match="/"><xsl:for-each select="//img"><xsl:if test= "@alt != ''">\par <xsl:value-of select="@alt"/> \par </xsl:if></xsl:for-each></xsl:template>

Or if you really need it as a template:

<xsl:template match="/"><xsl:apply-templates select="//img"/></xsl:template><xsl:template match="//img"><xsl:if test= "@alt != ''">\par <xsl:value-of select="@alt"/> \par </xsl:if></xsl:template>

Note: tested in FF.[/edit]

Edited by boen_robot
Link to comment
Share on other sites

Never mind. I knew it was something stupid, and sure enough it was.The code was right - the testcase was wrong.I had bad code - fixed it to the above - and thought I was testing my fixed version but was really still using the old bad code.sigh. Sorry to bother you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...