2010-05-21 32 views
4

我是新來這個,所以請多多包涵......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格式顯示

感謝很多是像之前的文本節點之前!

+0

很抱歉,但它是不是很清楚 - 你能不能,請提供更短(最短)的示例,演示問題了嗎?使用英語進行標記也不會傷害。 :) – 2010-05-21 17:57:15

+0

在我的答案中看到解決方案。 :) – 2010-05-21 18:19:11

回答

1

當XML最好以「聲明式」風格編寫時,您正在嘗試以「過程式」風格編寫XML。這些被稱爲「拉」和「推」處理。下面是使用你的數據爲例,可以幫助:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" version="1.0"> 
    <xsl:output method="html" encoding="iso-8859-1"/> 

    <xsl:template match="/"> 
    <rootNode> 
     <xsl:apply-templates select="docXML/PARRAFO[@tipo='parrafo']"/> 
    </rootNode> 
    </xsl:template> 

    <xsl:template match="PARRAFO[@tipo='parrafo']"> 
    <div class="parrafo"> 
    <xsl:apply-templates select="dato[etiqueta='Imagen']"/> 
    <xsl:apply-templates select="dato[etiqueta='Texto']"/> 
    </div> 
    </xsl:template> 

    <xsl:template match="dato[etiqueta='Imagen']"> 
    <image></image> 
    <!-- processing --> 
    </xsl:template> 

    <xsl:template match="dato[etiqueta='Texto']"> 
    <text></text> 
    <!-- processing --> 
    </xsl:template> 

</xsl:stylesheet> 
1

我完全不明白提供的XML文檔和問題的詳細信息(對不起,還沒有學過意大利語但:(),但是這裏是一個小例子,說明如何處理不以文檔順序處理的XML元素

這種轉變:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="/*"> 
    <nums> 
    <odd> 
     <xsl:apply-templates select="*[position() mod 2 = 1]"/> 
    </odd> 
    <even> 
     <xsl:apply-templates select="*[position() mod 2 = 1]"/> 
    </even> 
    </nums> 
</xsl:template> 
</xsl:stylesheet> 

當這個XML文檔施加:

<nums> 
    <num>01</num> 
    <num>02</num> 
    <num>03</num> 
    <num>04</num> 
    <num>05</num> 
    <num>06</num> 
    <num>07</num> 
    <num>08</num> 
    <num>09</num> 
    <num>010</num> 
</nums> 

處理並輸出第一奇數<num>元素,則偶數的

<nums> 
    <odd> 
     <num>01</num> 
     <num>03</num> 
     <num>05</num> 
     <num>07</num> 
     <num>09</num> 
    </odd> 
    <even> 
     <num>01</num> 
     <num>03</num> 
     <num>05</num> 
     <num>07</num> 
     <num>09</num> 
    </even> 
</nums> 

從這個例子的教訓:可以容易地實現非文檔序處理通過指定要處理的所需節點集,在相應的<xsl:apply-templates>指令中 - 在它們的select屬性中。