开发者

How to get XML node value in XSLT if we have got XML path in PARAM

开发者 https://www.devze.com 2023-04-09 21:53 出处:网络
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 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/201开发者_StackOverflow1 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.

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 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):

<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.


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 EXSLT functions:

<?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.

0

精彩评论

暂无评论...
验证码 换一张
取 消