2010-12-01 59 views
7

我的系統使用一個靜態的網站鏈接的XSL文件,如下圖所示:如何讀取.xsl文件中的.properties文件?

<xsl:template match="my_match"> 

    <xsl:variable name="variable1"> 
     <xsl:value-of select="sel1/Label = 'Variable1'"/> 
    </xsl:variable> 
    <xsl:copy-of select="sites:testPath('http://testsite.com/services/testService/v1.0', $fname, $lname, 
    $email , $zip, $phone, $comments, $jps, boolean($myvar), string(cust/@custID), string(@paID))"/> 
</xsl:template> 

我的問題是,如何讀取性能XSL文件中的文件(鍵值對)。所以在我的屬性文件(例如site.properties)我有一個名爲關鍵sitesite=testsite.com/services/testService/v1.0

我想在XSL即使用本網站鍵到位指定URL值的http://testsite.com/services /testService/v1.0。這樣做的原因是這個鏈接根據不同的環境而變化。

這可能嗎? 如果可能,請給出您的建議或示例代碼...如果這是不可能的...是否有任何解決方法?

+1

使用XSLT 2.0,可以使用`fn:unparsed-text()`來實現,但只要將值作爲參數發送到樣式表可能會更容易。 – 2010-12-01 15:58:46

+0

我完全不理解這個問題。請你解釋一下這是怎麼一回事?沒有輸入,也沒有指定所需的輸出。 – 2010-12-01 16:54:52

回答

7

由於概念證明:

輸入屬性文件:

# You are reading the ".properties" entry. 
! The exclamation mark can also mark text as comments. 
website = http://example.com 
language = English 
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces". 

樣式表:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:f="Functions" 
    version="2.0"> 

    <xsl:variable name="properties" select="unparsed-text('.properties')" as="xs:string"/> 

    <xsl:template match="/" name="main"> 
    <xsl:value-of select="f:getProperty('language')"/> 
    </xsl:template> 

    <xsl:function name="f:getProperty" as="xs:string?"> 
    <xsl:param name="key" as="xs:string"/> 
    <xsl:variable name="lines" as="xs:string*" select=" 
     for $x in 
     for $i in tokenize($properties, '\n')[matches(., '^[^!#]')] return 
      tokenize($i, '=') 
     return translate(normalize-space($x), '\', '')"/> 
    <xsl:sequence select="$lines[index-of($lines, $key)+1]"/> 
    </xsl:function> 

</xsl:stylesheet> 

f:getProperty('language')將返回 '英語'。

將此視爲概念證明,需要在很多方面進行改進,因爲它無法處理可以創建.properties文件的許多不同方式。

我相信亞歷杭德羅或迪米特里可能會改善很多次。

5

對於XSLT 1.0解決方案,您可以在XML文件中使用external (parsed) general entity,該文件將加載屬性文件作爲XML內容的一部分。

舉例來說,如果你有一個屬性文件這樣的,命名爲site.properties

foo=x 
site=http://testsite.com/services/testService/v1.0 
bar=y 

您可以創建一個簡單的XML文件,命名爲properties.xml說,「包裹」的屬性的內容文件並加載它使用外部解析的一般實體:

<!DOCTYPE properties [ 
    <!ENTITY props SYSTEM "site.properties"> 
]> 
<properties> 
&props; 
</properties> 

然後,你的XSLT內您使用document()功能可以加載properties.xml和獲得的價值對於一個給定的關鍵:

<?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" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:variable name="props" select="document('properties.xml')" /> 
    <xsl:template match="/"> 
     <output> 
      <example1> 
       <!--simple one-liner --> 
       <xsl:value-of select="substring-before(
             substring-after($props, 
                 concat('site','=')), 
             '&#xA;')" /> 
      </example1> 
      <example2> 
       <!--using a template to retrieve the value 
        of the "site" property --> 
       <xsl:call-template name="getProperty"> 
        <xsl:with-param name="propertiesFile" select="$props"/> 
        <xsl:with-param name="key" select="'site'"/> 
       </xsl:call-template> 
      </example2> 
      <example3> 
       <!--Another example using the template to retrieve 
        the value of the "foo" property, 
        leveraging default param value for properties --> 
       <xsl:call-template name="getProperty"> 
        <!--default $propertiesFile defined in the template, 
         so no need to specify --> 
        <xsl:with-param name="key" select="'foo'"/> 
       </xsl:call-template> 
      </example3> 
     </output> 

    </xsl:template> 

    <!--Retrieve a property from a properties file by specifying the key --> 
    <xsl:template name="getProperty"> 
     <xsl:param name="propertiesFile" select="$props"/> 
     <xsl:param name="key" /> 
     <xsl:value-of select="substring-before(
           substring-after($propertiesFile, 
               concat($key,'=')), 
           '&#xA;')" /> 
    </xsl:template> 

</xsl:stylesheet> 

當應用於任何XML輸入上面的樣式表會產生以下輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<output> 
    <example1>http://testsite.com/services/testService/v1.0</example1> 
    <example2>http://testsite.com/services/testService/v1.0</example2> 
    <example3>x</example3> 
</output> 

注:如果屬性文件的內容是「XML安全」這種策略只會工作。如果它包含字符,如&<,那麼加載properties.xml文件時將導致XML解析錯誤。