所以这可能是一个奇怪的请求,但我会试一试.我有一个xml文档
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ID>Site</ID>
<Object>
<ID>PostCode</ID>
<Type>div</Type>
<Class>display-label</Class>
<Value>PostCode</Value>
</Object>
<Object>
<ID>PostCodeValue</ID>
<Type>div</Type>
<Class>display-field</Class>
<Value>$PostCode</Value>
</Object>
</Page>
Run Code Online (Sandbox Code Playgroud)
我正在使用这个XSL将其转换为html页面
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:for-each select="Page/Object">
<<xsl:value-of select="Type"/>>
<xsl:value-of select="Value"/>
</<xsl:value-of select="Type"/>>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在尝试根据xml中的类型节点生成正确的html标记.问题是xsl中不接受"<",并且编码会阻止它被识别为标记.
有什么建议我会怎么做?
提前致谢
使用xsl:element在输出文档中创建元素节点.
<xsl:element name="{Type}" >
<xsl:value-of select="Value"/>
</xsl:element>
Run Code Online (Sandbox Code Playgroud)
注意:如果您不熟悉,name属性中的花括号可能看起来很奇怪.它是一个属性值模板,用于评估属性声明中的XPATH表达式.
应用于样式表:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:for-each select="Page/Object">
<xsl:element name="{Type}" >
<xsl:value-of select="Value"/>
</xsl:element>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)