如何在.xsl文件中读取.properties文件?

时间:2010-12-01 15:48:57

标签: xslt properties

我有一个XSL文件,它使用静态网站链接,如下所示:

<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)中,我有一个名为site的密钥,即site=testsite.com/services/testService/v1.0

我想使用此站点密钥代替在xsl中指定url值,即 http://testsite.com/services/testService/v1.0 。这样做的原因是此链接会根据不同的环境而变化。

这可能吗? 如果可能,请提供您的建议或示例代码......如果这是不可能的......有没有解决办法?

2 个答案:

答案 0 :(得分:7)

作为概念证明:

输入.properties文件:

# 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文件可以创作的许多不同方式。

我相信亚历杭德罗或迪米特里可能会多次改善这一点。

答案 1 :(得分:6)

对于XSLT 1.0解决方案,您可以在XML文件中使用external (parsed) general entity,该文件将属性文件作为XML内容的一部分加载。

例如,如果您有一个这样的属性文件,名为site.properties

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

您可以创建一个名为properties.xml的简单XML文件,该文件“包装”属性文件的内容并使用外部解析的通用实体加载它:

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

然后,在您的XSLT中,您可以使用properties.xml函数加载document()并获取给定密钥的值:

<?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解析错误。

相关问题