Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT生成XML中指定的html标记_Xslt - Fatal编程技术网

XSLT生成XML中指定的html标记

XSLT生成XML中指定的html标记,xslt,Xslt,所以这可能是一个奇怪的要求,但我会试一试。我有一个xml文档 <?xml version="1.0" encoding="utf-8" ?> <Page> <ID>Site</ID> <Object> <ID>PostCode</ID> <Type>div</Type> <Class>display-label</Class>

所以这可能是一个奇怪的要求,但我会试一试。我有一个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>

场地
邮政编码
div
显示标签
邮政编码
邮政编码值
div
显示字段
$邮政编码
我使用这个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">
          &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:element name="{Type}" >
<xsl:value-of select="Value"/>
</xsl:element>

注意:如果您不熟悉,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>


您需要将“XSLT 1.0”包装为最初发布的内容:“对于用于结果树中文本节点以外的其他对象的文本节点,禁用输出转义是一个错误。”xsl:element是实现这一点的最佳方法,但要回答实际问题,您需要使用上述xsl:text。我如何使用class值填充此元素您的
div
?您可以使用
(例如
foo
)以类似的方式向元素添加属性。我添加了问题的答案,还提供了一种替代样式,使用应用模板(更具声明性)而不是为每个子句嵌套(更具程序性)来实现相同的结果。
<?xml version="1.0" encoding="utf-8"?><!-- DWXMLSource="temp.xml" -->
<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:text disable-output-escaping="yes">&lt;</xsl:text><xsl:value-of select="Type"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
          <xsl:value-of select="Value"/>
          <xsl:text disable-output-escaping="yes">&lt;/</xsl:text><xsl:value-of select="Type"/><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>