XSLT 生成以 XML 指定的 html 标签 [英] XSLT to generate html tags specified in XML

查看:19
本文介绍了XSLT 生成以 XML 指定的 html 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这可能是一个奇怪的请求,但我会试一试.我有一个 xml 文档

So this may be a strange request but I'll give it a go. I have an xml document

<?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>

我正在使用此 XSL 将其转换为 html 页面

and I'm using this XSL to transform it into a html page

<?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">
          &lt;<xsl:value-of select="Type"/>&gt;
          <xsl:value-of select="Value"/>
          &lt;/<xsl:value-of select="Type"/>&gt;
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

如您所见,我正在尝试根据 xml 中的类型节点生成正确的 html 标记.问题是<"xsl 中不接受它,对其进行编码会阻止它被识别为标记.

As you can see I'm trying to generate the correct html tag dependent on the type node in the xml. The problem is "<" isn't accepted in the xsl and encoding it stops it from being recognised as a tag.

我有什么建议吗?

提前致谢

推荐答案

使用 xsl:element 在输出文档中创建一个元素节点.

Use xsl:element to create an element node in the output document.

<xsl:element name="{Type}" >
<xsl:value-of select="Value"/>
</xsl:element>

注意:如果您不熟悉,name 属性中的花括号可能看起来很奇怪.它是一个 属性值模板,用于在属性声明.

应用于您的样式表:

<?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>

这篇关于XSLT 生成以 XML 指定的 html 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆