如何读取 .xsl 文件中的 .properties 文件? [英] How to read a .properties file inside a .xsl file?
问题描述
我有一个使用静态网站链接的 XSL 文件,如下所示:
I have an XSL file which uses a a static website link as shown below:
<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
My question is that how to read a properties file(key value pair) in the xsl file. so in my properties file (e.g. site.properties) I have a key called site
i.e. site=testsite.com/services/testService/v1.0
我想使用此站点密钥代替在 xsl 中指定 url 值,即 http://testsite.com/services/testService/v1.0.这样做的原因是这个链接根据不同的环境而变化.
I want to use this site key in place of specifying url value in the xsl i.e. http://testsite.com/services/testService/v1.0. The reason for doing this is that this link changes depending on the various environments.
这可能吗?如果可能,请提供您的建议或示例代码...如果这不可能...是否有任何解决方法?
Is this possible? Please give your suggestions or a sample code if possible...Also if this is not possible...is there any work-around?
推荐答案
作为概念证明:
输入 .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')
将返回 'English'.
The f:getProperty('language')
will return 'English'.
将此视为概念证明,这需要在许多方面进行改进,因为它无法处理可以创作 .properties 文件的许多不同方式.
See this as a proof of concept, this needs to be improved in many ways since it does not handle many of the different ways a .properties file can be authored.
我相信 Alejandro 或 Dimitrie 可能会多次改进这一点.
I belive Alejandro or Dimitrie probably could improve this many times.
这篇关于如何读取 .xsl 文件中的 .properties 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!