0

I have a xml.in this xml I have a node named video like below

  <product>
 <id>676872</id>
  <weightingram>510</weightingram>
  <volume>0</volume>
  <discountgroup />
 <name>Product name (500 g)</name>
  <vat>10,49</vat>
  <webbestprice extra="webbestprice">0</webbestprice>
  <webreturn extra="webreturn">0</webreturn>
  <weboutdate extra="weboutdate">01-01-2013 00:00:00</weboutdate>
  <webaltitem extra="webaltitem" />
  <filters extra="filters">
    <ISP_WebItem FILTER="Type" FILTERNAME="type" UNITCODE=""/>
    <ISP_WebItem FILTER="Type2" FILTERNAME="500" UNITCODE="g"/>
  </filters>
  <videos extra="videos">
    <YoutubeVideoURL RowNumber="33" ProductID="676872" YoutubeUrl="http://www.youtube.com/watch?v=EjoEIHVk9qM" YoutubeImage="https://img.youtube.com/vi/EjoEIHVk9qM/2.jpg"/>
  </videos>
</product>

the above xml is getting by using

<textarea>
<xsl:copy-of select="."/>
</textarea>

I need to get the value of attribute YoutubeUrl from this xml.I tried something like

 <xsl:value-of select="./videos/YoutubeVideoURL[@YoutubeUrl] "/>

but it is not working.Thanks in advance for help.

4
  • You need to show more context in your XSL, one cannot guess at what context your are executing the xsl:value-of and therefore cannot even guess at what your problem is. Show the whole template. Commented Oct 4, 2013 at 5:25
  • I agree with @KevinBrown that you should have to provide your working XML contents so that others can get idea. Commented Oct 4, 2013 at 6:15
  • @Matthew Green please find the edit Commented Oct 4, 2013 at 11:18
  • @Arun; i have updated my ans an tested. Its retriving the value properly...Let me know what template you have set to retrive the YoutubeUrl value Commented Oct 4, 2013 at 17:36

3 Answers 3

1

You can write like this

Your sample XML file

<?xml version="1.0" encoding="UTF-8"?>
<videos extra="videos">
    <YoutubeVideoURL RowNumber="1" ProductID="12452" YoutubeUrl="http://www.youtube.com/watch?v=efhrtgdbo" YoutubeImage="https://img.youtube.com/vi/efhrtgdbo/2.jpg"/>
</videos>

And your sample XSL file

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="YoutubeVideoURL">
        <xsl:value-of select="@YoutubeUrl"/>
    </xsl:template>
</xsl:stylesheet>

Final output

http://www.youtube.com/watch?v=efhrtgdbo

That's it

Sign up to request clarification or add additional context in comments.

4 Comments

But I have the xslt,in this xslt <xsl:template match="product"> here i get the sample xml as i given in the question.here I need to get the YoutubeUrl value. </xsl:template>
But you provided only </video> xml. So based on your sample I can only provide that answer. You have to show your working XML.
It is correct ..I said I can't call <xsl:template match="YoutubeVideoURL"> inside <xsl:template match="product">
Try with this if you can access as per your XML structure : <xsl:value-of select="./videos/YoutubeVideoURL/@YoutubeUrl"/>
1

Try this code

<xsl:template match="YoutubeVideoURL">
   <xsl:value-of select="@YoutubeUrl"/>
</xsl:template>

(or)

if you have specified the template that is matching to "/" root then use the below syntax.

<xsl:template match="/">

 .....
   <textarea>
      <xsl:copy-of select="." />
   </textarea>
   <p>
   <xsl:value-of select="//videos//YoutubeVideoURL//@YoutubeUrl"/>
   </p>
</xsl:template>

Copy-of will copy the xml element specified in the select expression. So TextArea will have that xml element with its structure. And you are using value-of to retrive the xml attribute which needs an element selection. So, its not related.

You can use either of the solution mentioned in this ans.

5 Comments

I tried the second one .but not working. <xsl:value-of select="./videos/YoutubeVideoURL@YoutubeUrl"/>
can you try without "./"? Start from videos. And did you tried the first one?
<xsl:value-of select="videos/YoutubeVideoURL@YoutubeUrl"/>
@Viji Your second example is not a valid XPath expression. You are missing a /.
@Arun: You can use this <xsl:value-of select="videos/YoutubeVideoURL/@YoutubeUrl"/> , I missed / in that.
0

In your value-of you are looking for any videos/YoutubeVideoURL that have an attribute of @YoutubeUrl. If you are looking to get the value of @YoutubeUrl then you need to take it out of the predicate like so.

<xsl:value-of select="./videos/YoutubeVideoURL/@YoutubeUrl" />

This of course assumes that your paths and templates are already set up correctly to locate this value at it's current position.

4 Comments

Yes When i Test <<xsl:value-of select="./videos> it get the result.. <YoutubeVideoURL RowNumber="29" ProductID="80182" YoutubeUrl="youtube.com/watch?v=EjoEIHVk9qM" YoutubeImage="img.youtube.com/vi/EjoEIHVk9qM/2.jpg"> but Trying <xsl:value-of select="./videos/YoutubeVideoURL/@YoutubeUrl" /> it dosn't give any result? What is the Problem?
@Arun I don't know what the issue is without you providing more of your XML and XSL. When I take what you've shown for XML and transform it with my XSL that looks for the value I've specified I get the URL. Is the XML you've shown in an actual XML and not a result tree fragment?
yes I edited question please find.the xml is getting while <textarea><xsl:copy-of select="."/></textarea>
@Arun so you are copying the XML into a <textarea>? I'm not sure I understand. Can you show more of the XSL? That might help narrow down the problem. I think your XML must be a RTF but it's hard to tell based on the information. Of course this all assumes you are using XSLT 1.0 too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.