问题描述
我有XSLT 1.0标准.我有一个来自服务器的动态XML,它非常简单,而第二个XML作为配置.基于第一个(动态的),我必须从第二个中获取适当的节点信息.这是第一个文件:
I have XSLT 1.0 standard.I have one dynamic XML from server which is quite simple and second XML as configuration. Base on first one (which is dynamic) I have to pick up proper nodes information from second one.This is first document:
<?xml version="1.0" encoding="UTF-8" ?>
<response>
<response>SUCCESS</response>
<responsedata>
<hit>
<url>http://domain.com/page.html</url>
<id>2437</id>
<title>Page title</title>
<language>en</language>
...
...
</hit>
</responsedata>
</response>
第二个配置XML用于页脚,页眉按语言划分.像这样的东西:
Second configuration XML is for footer, header divided by languages. Something like that:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<set id="local">
<header>
<en>
<![CDATA[
<div id="header">
<p>English code</p>
</div>
]]>
</en>
<fr>
<![CDATA[
<div id="header">
<p>French code</p>
</div>
]]>
</fr>
</header>
</set>
</config>
我需要从第二个XML中提取适当的语言相关代码.我尝试了以下代码,但它不起作用:
I need pick up proper language depended code from second XML.I tried following code and it doesn't work:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" encoding="utf-8" indent="yes" doctype-system="about:legacy-compat" />
<xsl:variable name="configuration" select="document('settings.xml')/config/set[@id='local']" />
<xsl:variable name="lang" select="response/responsedata/hit/language" />
<xsl:template name="getvalueofnode">
<xsl:param name="path" />
<xsl:param name="context" select="$configuration" />
<xsl:choose>
<xsl:when test="contains($path,'/')">
<xsl:call-template name="getvalueofnode">
<xsl:with-param name="path"
select="substring-after($path,'/')" />
<xsl:with-param name="context"
select="$context/*[name()=substring-before($path,'/')]" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<p>value: <xsl:value-of select="$context/*[name()=$path]" disable-output-escaping="yes" /></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:element name="html">
<xsl:attribute name="lang"><xsl:value-of select="$lang" /></xsl:attribute>
<xsl:element name="head">
<xsl:attribute name="lang"><xsl:value-of select="$lang" /></xsl:attribute>
</xsl:element>
<xsl:element name="body">
<xsl:attribute name="lang"><xsl:value-of select="$lang" /></xsl:attribute>
<p>lang: <xsl:value-of select="$lang" /></p>
<p>
<xsl:call-template name="getvalueofnode">
<xsl:with-param name="path" select="concat('/header/',$lang)" />
</xsl:call-template>
</p>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
如果有人有任何建议或解决方案,那就太好了.
If some one has any suggestion or solution it will be fantastic.
推荐答案
由于您已经知道基于$lang
的元素的名称,因此可以一起消除getvalueofnode
模板.
Since you already know the name of the element based on $lang
, you can eliminate the getvalueofnode
template all together.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" encoding="utf-8" indent="yes" doctype-system="about:legacy-compat" />
<xsl:variable name="configuration" select="document('settings.xml')/config/set[@id='local']" />
<xsl:variable name="lang" select="response/responsedata/hit/language" />
<xsl:template match="/">
<html lang="{$lang}">
<head lang="{$lang}"/>
<body lang="{$lang}">
<p>lang: <xsl:value-of select="$lang" /></p>
<p>value: <xsl:value-of disable-output-escaping="yes" select="$configuration/header/*[name()=$lang]"/></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我也摆脱了所有的xsl:element
和xsl:attribute
.通常可以通过直接编码元素并使用 AVT(属性值模板).
I also got rid of all the xsl:element
and xsl:attribute
. These can normally be avoided by coding the elements directly and using AVT (attribute value templates) for the attributes.
上面的样式表使用您的XML输入文件(经过Saxon 6.5.5和Saxon-HE 9.3.0.5测试)产生以下输出:
The stylesheet above produces the following output using your XML input files (tested with Saxon 6.5.5 and Saxon-HE 9.3.0.5):
<!DOCTYPE html
SYSTEM "about:legacy-compat">
<html lang="en">
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body lang="en">
<p>lang: en</p>
<p>value:
<div id="header">
<p>English code</p>
</div>
</p>
</body>
</html>
这篇关于具有动态路径的XSLT文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!