java xml xslt dom dynamically-generated

java - Crear archivos xslt mediante programación



xml dom (3)

Como XSLT también es XML, simplemente puede usar la misma estrategia:

... Document document = documentBuilder.newDocument(); Element rootElement = document.createElement("xsl:stylesheet"); // adding attributes like namespaces etc... document.appendChild(rootElement); Element em = document.createElement("xsl:template"); em.setAttribute("match", "/");

y así...

Pero no es muy elegante. En su lugar, debería usar una biblioteca o un marco, debería encontrar fácilmente una búsqueda en Google.

Sé que puedo crear archivos xml mediante programación usando DOM api en java como el siguiente:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement("map"); document.appendChild(rootElement); Element em = document.createElement("string"); em.setAttribute("name", "FirstName"); ....

¿Pero hay alguna API para construir un árbol xslt ? (una API como Dom por ejemplo)

Necesito algo como esto:

<?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"> <xsl:template match="root"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="my-page"> <fo:region-body margin="1in"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="my-page"> <fo:flow flow-name="xsl-region-body"> <fo:block> <fo:external-graphic width="100pt" height="100pt" content-width="50pt" content-height="50pt" src="images/shopping-cart_100.jpg"/> </fo:block> <fo:block>Good Morning, <xsl:value-of select="name" />!</fo:block> <fo:block> <fo:table> <fo:table-body> <fo:table-row> <fo:table-cell border="solid 1px black" text-align="center" font-weight="bold"> <fo:block>

y:

<xsl:for-each select="./friend"> <fo:table-row> <fo:table-cell border="solid 1px black" text-align="center"> <fo:block> <xsl:value-of select="position()" /> </fo:block> </fo:table-cell> <fo:table-cell border="solid 1px black" text-align="center"> <fo:block> <xsl:value-of select="name" /> </fo:block> </fo:table-cell> <fo:table-cell border="solid 1px black" text-align="center">

Gracias por adelantado.


Puede crear un XSLT de la misma manera que crea un archivo XML, ya que los XSLT son archivos XML.

Sin embargo, si tiene que lidiar mucho con las transformaciones XML / XSLT, Apache Cocoon 3 tiene un sistema de tuberías XML / XSLT muy liviano para usar como biblioteca en lugar de tratar todas las transformaciones XML dom stuff y XSLT manualmente.


DOM es una forma bastante engorrosa de crear XML.

Hay una forma mucho mejor: usar XSLT.

Cuanto más complejo sea el XML, mayor será la ganancia al usar XSLT en lugar de DOM para crearlo.

No hay ninguna razón por la que no pueda usar XSLT para crear XSLT (incluso hay una declaración especial xsl: namespace-alias para hacerlo un poco más fácil - buscando xsl: namespace-alias mostrará ejemplos de su uso.