user3216544
user3216544

Reputation: 25

Select according to key (element with namespace)

I am relatively new to XSLT I need to select the author elements (key:aid) according to which post using pauthorid of the current post.

The XML of the author

<a:authors>
    <a:author aid="a1">
        <a:name>Brian Muscat</a:name>
        <a:username>bmuscat</a:username>
        <a:password>abc123</a:password>
        <a:email>[email protected]</a:email>
    </a:author>
</a:authors>

The xml of the post

<posts>
    <post pid="p1">
        <ptitle>CLOUD COMPUTING</ptitle>
        <pfeatureimage>aaig.jpg</pfeatureimage>
        <ptext xml:lang="en">text</ptext>
        <pdate>25/06/2013</pdate>
        <pimg>cloud.jpg</pimg>
        <pimg>cloud.jpg</pimg>
        <pauthorid>a1</pauthorid>

    </post>
    </posts>

The xslt I have written till now

    <xsl:for-each select="posts/post">
    <div class="post">
    <div style="margin-top:20px; margin-right:20px; float:right;"> <span class="text" style="float:right;">Posted On:<xsl:value-of select="pdate"/></span></div>

        <h3><xsl:value-of select="ptitle"/></h3>
        <div style="padding:10px; height:60px; margin-top:-20px;">
        <span class="text"><xsl:value-of select="ptext"/>

        <xsl:variable name="aid" select="pauthorid" />
      <!--The Problem is here-->
 <xsl:for-each select="//a:authors/a:author[@aid=$aid]">
                    <xsl:value-of select="a:name" />
        </xsl:for-each>     
          </span>
         </div>
    </div>
    <br />
</xsl:for-each>

Upvotes: 0

Views: 37

Answers (1)

parakmiakos
parakmiakos

Reputation: 3020

I am guessing that you are talking about a single xml file. Your code is correct after you add stylesheet and template tags. You just need to declare what namespace the "a" prefix corresponds to (here is a sample declaration).

I have changed your for-each statement to "//posts/post" so that I could test, this solely depends on your initial xml and xsd.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:a="http://www.tibco.com/schemas/test/XSL_test/localschema" version="1.0">
  <xsl:template match="/">
    <xsl:for-each select="//posts/post">
    <div class="post">
    <div style="margin-top:20px; margin-right:20px; float:right;"> 
        <span class="text" style="float:right;">Posted On:<xsl:value-of select="pdate"/>        
    </span></div>

    <h3><xsl:value-of select="ptitle"/></h3>
    <div style="padding:10px; height:60px; margin-top:-20px;">
    <span class="text"><xsl:value-of select="ptext"/>

    <xsl:variable name="aid" select="pauthorid" />
     <xsl:for-each select="//a:authors/a:author[@aid=$aid]">
        <xsl:value-of select="a:name" />
     </xsl:for-each>     
    </span>
   </div>
</div>
<br />
</xsl:for-each>
</xsl:stylesheet>

Upvotes: 1

Related Questions