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

Sam*_*tra 7 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>
Run Code Online (Sandbox Code Playgroud)

我的问题是如何读取xsl文件中的属性文件(键值对).所以在我的属性文件(例如site.properties)中我有一个名为siteie 的键site=testsite.com/services/testService/v1.0

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

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

Per*_*r T 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".
Run Code Online (Sandbox Code Playgroud)

样式表:

<?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>
Run Code Online (Sandbox Code Playgroud)

f:getProperty('language')会返回"英语".

将此视为概念验证,需要以多种方式对其进行改进,因为它无法处理.properties文件可以创作的许多不同方式.

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

  • @Dimitre:这是一个简单的问题.我不知道你为什么要大肆宣传它.我不必告诉你整体架构或类似的东西.简单的问题是如何读取xsl文件中的属性文件(键值对).所以在我的属性文件(site.properties)中,我有一个名为"site = http://testsite.com/services/testService/v1.0"的密钥,我想使用此站点密钥代替在中指定url值XSL.如果你不知道答案那么好.只是不要让其他人更复杂.我感谢你的帮助.谢谢Per-T,我会尝试你的解决方案...... (2认同)

Mad*_*sen 6

对于XSLT 1.0解决方案,您可以在XML文件中使用外部(已解析)通用实体,该实体将加载属性文件作为XML内容的一部分.

例如,如果您有这样的属性文件,则命名为site.properties:

foo=x
site=http://testsite.com/services/testService/v1.0
bar=y
Run Code Online (Sandbox Code Playgroud)

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

<!DOCTYPE properties [
  <!ENTITY props SYSTEM "site.properties">
]>
<properties>
 &props;
</properties>
Run Code Online (Sandbox Code Playgroud)

然后,在您的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>
Run Code Online (Sandbox Code Playgroud)

当应用于任何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>
Run Code Online (Sandbox Code Playgroud)

注意:此策略仅在属性文件的内容为"XML安全"时才有效. 如果它包含字符,&或者<properties.xml加载文件时会导致XML解析错误.