我有一个包含信息的XML文件,例如:
<letter>
<name>Test</name>
<age>20</age>
<me>Me</me>
</letter>
Run Code Online (Sandbox Code Playgroud)
然后我有一个文本模板,如:
Dear $name,
some text with other variables like $age or $name again
greatings $me
Run Code Online (Sandbox Code Playgroud)
当使用xslt将XML转换为纯文本字母时,我可以使用以下内容:
<xsl:text>Dear </xsl:text><xsl:value-of select="name"/><xsl:text>
some text with other variables like </xsl:text>
<xsl:value-of select="age"/><xsl:text> or </xsl:text>
<xsl:value-of select="name"/><xsl:text> again
greatings </xsl:text><xsl:value-of select="me"/>
Run Code Online (Sandbox Code Playgroud)
但是当我得到越来越多的变量和更多的文本时,这就变成了进入和维持的噩梦.
有没有办法用xslt以更干净的方式做到这一点?我更愿意,如果我可以使用我上面用作示例的文本模板,并将$ name和$ age替换为正确的值.
小智 6
这个样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my">
<xsl:output method="text"/>
<xsl:preserve-space elements="my:layout"/>
<my:layout>Dear <name/>,
some text with other variables like <age/> or <name/> again
greatings <me/></my:layout>
<xsl:variable name="vData" select="/"/>
<xsl:template match="/">
<xsl:apply-templates select="document('')/*/my:layout/node()"/>
</xsl:template>
<xsl:template match="*/*">
<xsl:value-of select="$vData//*[name()=name(current())]"/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出:
Dear Test,
some text with other variables like 20 or Test again
greatings Me
Run Code Online (Sandbox Code Playgroud)
注意:对于更复杂的人口模式(即迭代),请查看以下帖子:Sitemesh与XSLT的功能类似?和动态内容区域的XSLT布局
| 归档时间: |
|
| 查看次数: |
3111 次 |
| 最近记录: |