XSLT to generate html tags specified in XML

后端 未结 2 1278
栀梦
栀梦 2021-01-15 08:23

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



  Si         


        
相关标签:
2条回答
  • 2021-01-15 08:41

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

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

    Note: the curly braces in the name attribute may look strange, if you aren't familiar. It is an Attribute Value Template used to evaluate an XPATH expression inside of an attribute declaration.

    Applied to your stylesheet:

    <?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>
    
    0 讨论(0)
  • 2021-01-15 08:46

    you need to wrap your "<" in a text node with disable-output-escaping like this:

    <?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>
    
    0 讨论(0)
提交回复
热议问题