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!!
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.
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 W3C XSLT specification defines that the URL typically is loaded and parsed only once — the function is stable, 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):
Here you obtain the document using only one, single call to the
document()function.