Desenhar o XML genérico como HTML
escrevi um serviço web (em Java) que produz como resultado um documento XML (bastante complexo).
Sou obrigado a devolver uma representação HTML do documento, se solicitado. Não tem que ser chique, exibindo o código XML com alguma formatação básica, como indenting corretamente elementos faria.
Como faço isto?4 answers
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Evaluate Attributes -->
<xsl:template match="@*">
<span class="attribute">
<span class="name">
<xsl:value-of select="name()"/>
</span>="<span class="value"><xsl:value-of select="." /></span>"
</span>
</xsl:template>
<!-- Evaluate Elements -->
<xsl:template match="*" priority="10">
<div class="element">
<!-- First, create the opening tag with the attributes -->
<<span class="name"><xsl:value-of select="name()"/></span><xsl:apply-templates select="@*"/>>
<!-- Then, add children -->
<xsl:apply-templates select="node()"/>
<!-- Finally, add the closing tag -->
</<span class="name"><xsl:value-of select="name()"/></span>>
</div>
</xsl:template>
<!-- Just copy everything else (text, comments, etc.) -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
E juntar alguns CSS:
.element{margin-left:4em;font-size:14px;}
.element .name{color:blue;}
.attribute{margin-left:8px}
Actualização:
<!-- Root node -->
<xsl:template match="/">
<div class="element">
<<span class="name"><xsl:value-of select="name()"/></span><xsl:apply-templates select="@*"/>
<!-- Grab all namespaces and declare them. distinct-values() is XPath 2.0, however. -->
<xsl:for-each select="distinct-values(//namespace::*)">
xmlns:<xsl:value-of select="name()" />="<xsl:value-of select="." />"
</xsl:for-each>
>
<xsl:apply-templates select="node()"/>
</<span class="name"><xsl:value-of select="name()"/></span>>
</div>
</xsl:template>
Pode equipar o seu documento XML com uma folha de estilo CSS genérica:
<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
<?xml-stylesheet type="text/css" href="mystyle.css" ?>
<!-- your main document -->
Ou, em alternativa, pode fornecer uma folha de estilo XSLT que transforma o seu documento num documento HTML:
<?xml version="1.0" encoding="iso-8859-1" standalone="no" ?>
<?xml-stylesheet type="text/xsl" href="transform_to_html.xsl" ?>
<!-- your main document -->
A abordagem anterior pode ser mais simples e direta, já que a maioria dos navegadores modernos exibem árvores arbitrárias de documentos XML hoje em dia. A desvantagem é que você precisa definir todos os atributos estilísticos básicos (display
, mais notavelmente) para cada elemento tipo.
Parece algo para o qual XLST pode ser útil.
Que tipo de ambiente/bibliotecas você tem disponível?Se eu entender a sua pergunta, você gostaria de transformar o seu documento XML em HTML. Existem muitas técnicas para fazê-lo, incluindo analisar o XML e gerar HTML à medida que você vai. Mas o que eu recomendo é usar XSLT. O XSLT permite-lhe facilmente transformar XML em qualquer outro XML (incluindo o XHTML).
Referências
http://www.w3schools.com/xsl/
http://zvon.org/comp/m/xslt.html