1
votes

Let's say I have the following XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Reviews>
    <REVIEW ATTR1="some text" ATTR2="some other text">
</REVIEW>
</Reviews>

I am having a hard time getting my xsl file to work. Here's the relevant bit:

          <div style="width:300px; float:left; padding:10px 20px 0 0; vertical-align:top">
            <div style="font-size:14px; padding-left:3px">
              <xsl:for-each select="Reviews/REVIEW">
                <xsl:if test="REVIEW/ATTR1 != ''">
                  Something: <xsl:value-of select="REVIEW/ATTR1" />
                  <br/>
                </xsl:if>
              </xsl:for-each>
            </div>
          </div>

Even though there's something in ATTR1 the if test comes back false. I've tried using Reviews/REVIEW/ATTR1 and just ATTR1 but nothing works. I am not even sure this is the way to get attributes from elements. I looked up W3 schools but their tutorial does not mention how to get attributes. Extremely new to XML and brand new to XSL. Thanks.

1

1 Answers

1
votes

There are two problems here. Firstly, to access an attribute you need to prefix it with a @ symbol. Secondly, within your xsl:for-each your current context is the REVIEW node, so you don't need to include that again in the expressions.

Try this instead

<xsl:if test="@ATTR1 != ''">
     Something: <xsl:value-of select="@ATTR1" />
     <br/>
</xsl:if>