How to get XML node value in XSLT if we have got XML path in PARAM
-
I am using XSLT 1.0 and have below sample code: In my XSLT, I have got a param which has my XML path <xsl:param name="sitespath"/> I know I can load it as document and then further get the values accordingly, like below <xsl:variable name="siteInfoPath" select="document($sitespath)/sitedata/region/site/language"/> The above siteInfoPath XSLT varaible is loading the document with /sitedata/region/site/language data, however now I want to take PublishDate, below is the XML sample format <?xml version="1.0"?> <sitedata> <resources> <WorldwideSites>Worldwide sites</WorldwideSites> <PublishedDate>10/3/2011 9:12:35 AM</PublishedDate> </resources> <region code="global" title="Global"> <site defaultLanguage="en" id="tcm:0-233-1" url="/english" countryCode="" title="" order="1"> <language code="en" pubId="tcm:0-233-1" pubPath="\english" Culture="en-GB" ShortDate="dd MMM yy" ShortDateShortDay="ddd dd MMM yy" ShortDateTime="dd MMM yy, HH:mm" LongDate="d MMMM, yyyy" LongDateTime="d MMMM, yyyy, HH:mm" LongDateExtendedShortDay="ddd dd MMM, yyyy" LongDateExtended="dddd, d MMMM, yyyy" LongDateExtendedTime="dddd, d MMMM, yyyy, HH:mm" MonthYear="MMMM, yyyy" OmniturePrefix="ek global:en:" OmnitureReportSuite="emirnewglobalenglish,emirnewibems" OmnitureDevReportSuite="emirglobalendev" sifr="Y" localTitle="" url="/english" mobileRedirect="true" flightStatusAlert="true" GoLiveDate="20071110" targetHost="http://fly1.com" hpSearchF="Yes" hpHotelsCars="Yes" hpMYB="Yes" hpOLCI="Yes" hpFStatus="Yes" hpServicesF="Yes">English</language> </site> Do I need to use another variable and need to load document like this <xsl:variable name="siteInfoDate" select="document($sitespath)/sitedata/resources/PublishedDate"/> I don't want to load same xml again...please suggest!!
-
Answer:
Dynamic Xpath evaluation in general requires using an extension function --both in XSLT 1.0 and in XSLT 2.0. This is not a good idea, because you may or maynot find such an extension and may end up needing to write your own. Also, the code's portability is ruined. Do I need to use another variable and need to load document like this <xsl:variable name="siteInfoDate" select="document($sitespath)/sitedata/resources/PublishedDate"/> I don't want to load same xml again...please suggest!! This is actually the best way to solve the problem. It is a widespread misconception that issuing more than one calls to the document() function with the same URL loads and parses the same document more than once. The http://www.w3.org/TR/xpath-functions/#func-doc, regardless how many times the document() function is called. Therefore, there isn't any loss of efficiency. To eliminate your fears, do (this will help you sleep well, but isn't at all necessary): <xsl:variable name="vDoc" select="document($sitespath)"/> <xsl:variable name="siteInfoDate" select="$vDoc/sitedata/resources/PublishedDate"/> <xsl:variable name="siteInfoPath" select="$vDoc/sitedata/region/site/language"/> Here you obtain the document using only one, single call to the document() function.
Manoj Singh at Stack Overflow Visit the source
Other answers
In general in XSL 1, you would have to use an extension function for this as there is not a direct way to accomplish this. This is somewhat dependent on the actual parser you are using, but here is an example that works with Xalan which supports optional http://exslt.org/: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dyn="http://exslt.org/dynamic"> <xsl:param name="sitespath">/sitedata/region/site/language</xsl:param> <xsl:template match="/"> <xsl:message> <xsl:value-of select="dyn:evaluate($sitespath)/@code"/> </xsl:message> </xsl:template> </xsl:stylesheet> The evaluate() function will dynamically evaluate a XPath string and the example above will simply print it out the value of attribute code under whatever XPath the value of sitespath evaluates to. If you are not using Xalan, then what you need to do may be different. For example, for Saxon parser all you need to do is change the namespace dyn URI to http://saxon.sf.net/ for more recent versions, or to http://icl.com/saxon for older ones.
ivantod
Related Q & A:
- How to parse single xml node in windows phone?Best solution by Stack Overflow
- How to get the colour name by RGB value?Best solution by Stack Overflow
- How to get the column value when a row is selected in wpf listview?Best solution by Stack Overflow
- How to get real path of an image in Java?Best solution by Stack Overflow
- How do i get an email alert sound like aol "you've got mail?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.