我是新來這個,所以請多多包涵......XSLT應用模板在不同的順序XML閱讀
如果我們有以下XML片段:
<docXML>
<PARRAFO orden='1' tipo='parrafo'>
<dato>
<etiqueta>Título</etiqueta>
<tipo>TextBox</tipo>
<valor>¿Cuándo solicitar el consejo genético?</valor>
<longitud>1500</longitud>
<comentario></comentario>
<enlace></enlace>
<target_enlace>I</target_enlace>
</dato>
<dato>
<etiqueta>Texto</etiqueta>
<tipo>Resumen</tipo>
<valor>Resumen text</valor>
<longitud>8000</longitud>
<comentario></comentario>
<enlace></enlace>
<target_enlace></target_enlace>
</dato>
<dato>
<etiqueta>Imagen</etiqueta>
<tipo>TextBox</tipo>
<valor>http://url/Imagenes/7D2BE6480CF4486CA288A75932606181.jpg</valor>
<longitud>1500</longitud>
<comentario></comentario>
<enlace></enlace>
<target_enlace>I</target_enlace>
</dato>
</PARRAFO>
<PARRAFO orden='1' tipo='parrafo'>
<dato>
<etiqueta>Título</etiqueta>
<tipo>TextBox</tipo>
<valor>TextBox text</valor>
<longitud>1500</longitud>
<comentario></comentario>
<enlace></enlace>
<target_enlace>I</target_enlace>
</dato>
<dato>
<etiqueta>Texto</etiqueta>
<tipo>Resumen</tipo>
<valor>Resumen text</valor>
<longitud>8000</longitud>
<comentario></comentario>
<enlace></enlace>
<target_enlace></target_enlace>
</dato>
</PARRAFO>
</docXML>
..我去
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xsl" exclude-result-prefixes="msxsl">
<xsl:output method="html" encoding="iso-8859-1"/>
<xsl:template match="/">
<xsl:variable name="xml-doc-parrafo" select="documentoXML/PARRAFO"/>
<!-- PARRAFOS -->
<xsl:choose>
<xsl:when test="count($xml-doc-parrafo)>0">
<div class="seccion_1">
<xsl:for-each select="$xml-doc-parrafo">
<xsl:choose>
<xsl:when test="self::node()[@tipo = 'parrafo']">
<div class="parrafo">
<xsl:for-each select="self::node()[@tipo = 'parrafo']/dato">
<xsl:variable name="dato" select="self::node()[@tipo = 'parrafo']/dato"/>
<xsl:variable name="nextdato" select="following::dato[1]/@etiqueta"/>
<xsl:choose>
<xsl:when test="etiqueta = 'Título'">
<xsl:call-template name="imprimeTituloParrafo">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="etiqueta = 'Subtitulo'">
<xsl:call-template name="imprimeSubtituloParrafo">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="etiqueta = 'Imagen'">
<xsl:call-template name="imprimeImagenParrafo">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="etiqueta = 'Pie Imagen'">
<xsl:call-template name="imprimePieImagenParrafo">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="etiqueta = 'Texto'">
<xsl:call-template name="imprimeTextoParrafo">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:when test="etiqueta = 'Pie Parrafo'">
<xsl:call-template name="imprimePieParrafo">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</div>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</div>
</xsl:when>
<!-- si no hay resultados -->
<xsl:otherwise>
<br></br>
<p style="text-align:center;">El documento no contiene datos.</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="imprimeTituloParrafo">
<xsl:param name="etiqueta"></xsl:param>
<xsl:param name="valor"></xsl:param>
<xsl:param name="longitud"></xsl:param>
<xsl:param name="enlace"></xsl:param>
<xsl:param name="target_enlace"></xsl:param>
<h2 class="titulo">
<xsl:choose>
<xsl:when test="string-length($enlace) > 0">
<xsl:call-template name="imprimeEnlace">
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$valor"/>
</xsl:otherwise>
</xsl:choose>
</h2>
</xsl:template>
<xsl:template name="imprimeSubtituloParrafo">
<xsl:param name="etiqueta"></xsl:param>
<xsl:param name="valor"></xsl:param>
<xsl:param name="longitud"></xsl:param>
<xsl:param name="enlace"></xsl:param>
<xsl:param name="target_enlace"></xsl:param>
<h3 class="subtitulo">
<xsl:choose>
<xsl:when test="string-length($enlace) > 0">
<xsl:call-template name="imprimeEnlace">
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$valor"/>
</xsl:otherwise>
</xsl:choose>
</h3>
</xsl:template>
<xsl:template name="imprimeTextoParrafo">
<xsl:param name="etiqueta"></xsl:param>
<xsl:param name="valor"></xsl:param>
<xsl:param name="longitud"></xsl:param>
<xsl:param name="enlace"></xsl:param>
<xsl:param name="target_enlace"></xsl:param>
<div class="texto">
<p class="texto">
<xsl:copy-of select="$valor/node()"/>
</p>
</div>
</xsl:template>
<xsl:template name="imprimeImagenParrafo">
<xsl:param name="etiqueta"></xsl:param>
<xsl:param name="valor"></xsl:param>
<xsl:param name="longitud"></xsl:param>
<xsl:param name="comentario"></xsl:param>
<xsl:param name="enlace"></xsl:param>
<xsl:param name="target_enlace"></xsl:param>
<xsl:choose>
<xsl:when test="string-length($enlace) = 0">
<xsl:call-template name="imprimeImagen">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<a>
<xsl:choose>
<xsl:when test="$target_enlace/node() = 'E'">
<xsl:attribute name="target">
<xsl:text>_blank</xsl:text>
</xsl:attribute>
</xsl:when>
<xsl:when test="$target_enlace/node() = 'I'">
<xsl:attribute name="target">
<xsl:text>_self</xsl:text>
</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:attribute name="href">
<xsl:value-of select="$enlace"/>
</xsl:attribute>
<xsl:call-template name="imprimeImagen">
<xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
<xsl:with-param name="valor" select="valor"></xsl:with-param>
<xsl:with-param name="longitud" select="longitud"></xsl:with-param>
<xsl:with-param name="comentario" select="comentario"></xsl:with-param>
<xsl:with-param name="enlace" select="enlace"></xsl:with-param>
<xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
</xsl:call-template>
</a>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="imprimeImagen">
<xsl:param name="etiqueta"></xsl:param>
<xsl:param name="valor"></xsl:param>
<xsl:param name="longitud"></xsl:param>
<xsl:param name="comentario"></xsl:param>
<xsl:param name="enlace"></xsl:param>
<xsl:param name="target_enlace"></xsl:param>
<div class="imagen_pie">
<img>
<xsl:attribute name="src">
<xsl:value-of select="$valor"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="$comentario"/>
</xsl:attribute>
</img>
</div>
</xsl:template>
<xsl:template name="imprimeEnlace">
<xsl:param name="valor"></xsl:param>
<xsl:param name="longitud"></xsl:param>
<xsl:param name="comentario"></xsl:param>
<xsl:param name="enlace"></xsl:param>
<xsl:param name="target_enlace"></xsl:param>
<a>
<xsl:choose>
<xsl:when test="$target_enlace/node() = 'E'">
<xsl:attribute name="target">
<xsl:text>_blank</xsl:text>
</xsl:attribute>
</xsl:when>
<xsl:when test="$target_enlace/node() = 'I'">
</xsl:when>
<xsl:when test="$target_enlace/node() = 'D'">
</xsl:when>
</xsl:choose>
<xsl:attribute name="href">
<xsl:value-of select="enlace"/>
</xsl:attribute>
<xsl:value-of select="$valor"/>
</a>
</xsl:template>
....
</xsl:stylesheet>
我需要首先應用模板圖像(如果存在:以模板依賴於每個節點的「拿督」標籤「etiqueta」的「PARRAFO」的值通過以下XSLT適用於每一節這個「PA RRAFO「)‘imagen畫質’只是文本之前‘Texto’
現在首先應用模板的文本,然後將圖像,因爲它以XML格式顯示
感謝很多是像之前的文本節點之前!
很抱歉,但它是不是很清楚 - 你能不能,請提供更短(最短)的示例,演示問題了嗎?使用英語進行標記也不會傷害。 :) – 2010-05-21 17:57:15
在我的答案中看到解決方案。 :) – 2010-05-21 18:19:11