2011-02-08 23 views
5

Tengo un poco de contenido html dentro de mi XML. Anteriormente solo podía usar <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> para llevar el contenido al área correcta. Un nuevo requisito es convertir el primer <tr> dentro de cada tabla <tbody> en un conjunto de thead/tr/th.Convierta la primera fila de la tabla HTML en una fila de encabezado para cada tabla usando XSLT

estoy confundido sobre cómo convertir, de hecho, ni siquiera orilla dónde empezar:

...

<customField name="mainContent" type="Html"> 
    <html> 
     <h1>Page Heading</h1> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <table cellspacing="0" cellpadding="0" summary="" border="0"> 
      <tbody> 
       <tr> 
        <td>Heading 1</td> 
        <td>Heading 2</td> 
        <td>Heading 3</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
      </tbody> 
     </table> 
    </html> 
</customField> 
... 

en:

... 
<customField name="mainContent" type="Html"> 
    <html> 
     <h1>Page Heading</h1> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <table cellspacing="0" cellpadding="0" summary="" border="0"> 
      <thead> 
       <tr> 
        <th>Heading 1</th> 
        <th>Heading 2</th> 
        <th>Heading 3</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
      </tbody> 
     </table> 
    </html> 
</customField> 
... 
+0

Buena pregunta, 1. Vea mi respuesta para una solución completa y breve, utilizando el patrón de diseño XSLT más fundamental y poderoso: la regla de identidad anulada. –

Respuesta

1

tengo algo de HTML contenido dentro de mi XML. Anteriormente solo podía usar <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> para extraer el contenido al área correcta . Un nuevo requisito es convertir el primer <tr> dentro de cada tabla <tbody> en un conjunto de thead/tr/th.

Esta transformación:

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

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

<xsl:template match="tbody/tr[1]"> 
    <thead> 
    <tr> 
     <xsl:apply-templates/> 
    </tr> 
    </thead> 
</xsl:template> 

<xsl:template match="tbody/tr[1]/td"> 
    <th><xsl:apply-templates/></th> 
</xsl:template> 
</xsl:stylesheet> 

cuando se aplica en el documento XML proporcionado:

<customField name="mainContent" type="Html"> 
    <html> 
     <h1>Page Heading</h1> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <table cellspacing="0" cellpadding="0" summary="" border="0"> 
      <tbody> 
       <tr> 
        <td>Heading 1</td> 
        <td>Heading 2</td> 
        <td>Heading 3</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
      </tbody> 
     </table> 
    </html> 
</customField> 

produce exactamente, resultado correcto deseada:

<customField name="mainContent" type="Html"> 
    <html> 
     <h1>Page Heading</h1> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <table cellspacing="0" cellpadding="0" summary="" border="0"> 
     <tbody> 
      <thead> 
       <tr> 
        <th>Heading 1</th> 
        <th>Heading 2</th> 
        <th>Heading 3</th> 
       </tr> 
      </thead> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
     </tbody> 
     </table> 
    </html> 
</customField> 

Do nota:

Se utiliza el patrón de diseño "regla de identidad overriden". Este es el patrón de diseño XSLT más fundamental y poderoso.

ACTUALIZACIÓN:

Como notado por Flynn1179, la definición de la OP del problema (arriba) es incompatible con la salida que ofrece como resultado querido. En esta salida, no solo es el primer tr dentro del tbody convertido a thead/tr (y sus hijos td en th), sino que el thead se mueve fuera del tbody.

En caso de que esto es realmente lo que quiere el OP, aquí se modifica solución también para este caso:

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

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

<xsl:template match="tbody/tr[1]"> 
    <thead> 
    <tr> 
    <xsl:apply-templates/> 
    </tr> 
    </thead> 
    <tbody> 
    <xsl:apply-templates 
     select="following-sibling::tr"/> 
    </tbody> 
</xsl:template> 

<xsl:template match="tbody/tr[1]/td"> 
    <th> 
    <xsl:apply-templates/> 
    </th> 
</xsl:template> 

<xsl:template match="tbody"> 
    <xsl:apply-templates select="tr[1]"/> 
</xsl:template> 
</xsl:stylesheet> 

cuando se aplica en el mismo documento XML, el resultado es:

<customField name="mainContent" type="Html"> 
    <html> 
     <h1>Page Heading</h1> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <table cellspacing="0" cellpadding="0" summary="" border="0"> 
     <thead> 
      <tr> 
       <th>Heading 1</th> 
       <th>Heading 2</th> 
       <th>Heading 3</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
      <tr> 
       <td>sample</td> 
       <td>sample</td> 
       <td>sample</td> 
      </tr> 
     </tbody> 
     </table> 
    </html> 
</customField> 
+0

+1. Correcto y lleno – Flack

+0

+1 Mejor respuesta. –

+0

@Alejandro y @Flack: Gracias. Sí, esta solución es completamente "push", mientras que la otra solución es pull style ... En otro tema, creo que le interesaría leer mi última publicación en el blog: "The Binary Search Tree Data Structure-having fun with XPath 3.0 "en: http://dnovatchev.wordpress.com/2011/02/08/the-binary-search-tree-data-structurehaving-fun-with-xpath-3-0/ –

0

Pruebe esto:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="tbody"> 
    <xsl:element name="thead"> 
    <xsl:apply-templates select="tr[1]" /> 
    </xsl:element> 
    <xsl:element name="tbody"> 
    <xsl:apply-templates select="tr[position()!=1]" /> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="tr[1]/td"> 
    <xsl:element name="th"> 
    <xsl:apply-templates /> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

Es simplemente reemplazar s su elemento tbody existente con un thead que contiene la primera fila, y un tbody que contiene todos menos el primero, y luego reemplaza todos los elementos td en el primer tr con th en su lugar.

+0

Gracias Flynn1179 por enterarse de cómo pedí la salida. – ConfusedMuch

0

Sólo por diversión, esta hoja de estilo:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="node()|@*" name="identity"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="tbody"> 
     <xsl:apply-templates mode="header"/> 
     <xsl:call-template name="identity"/> 
    </xsl:template> 
    <xsl:template match="tr[1]"/> 
    <xsl:template match="tr" mode="header"/> 
    <xsl:template match="tr[1]" mode="header"> 
     <thead> 
      <xsl:call-template name="identity"/> 
     </thead> 
    </xsl:template> 
    <xsl:template match="tr[1]/td"> 
     <th> 
      <xsl:apply-templates select="node()|@*"/> 
     </th> 
    </xsl:template> 
</xsl:stylesheet> 

Salida:

<customField name="mainContent" type="Html"> 
    <html> 
     <h1>Page Heading</h1> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <p>Gusto te minim tempor elit quam. Dolore vel accumsan parum option me. Demonstraverunt congue nisl soluta tincidunt seacula. Soluta saepius demonstraverunt praesent claritatem mutationem. Modo te ullamcorper vel augue veniam. Nunc investigationes dolor iriure typi in.</p> 
     <table cellspacing="0" cellpadding="0" summary="" border="0"> 
      <thead> 
       <tr> 
        <th>Heading 1</th> 
        <th>Heading 2</th> 
        <th>Heading 3</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>sample</td> 
        <td>sample</td> 
       </tr> 
      </tbody> 
     </table> 
    </html> 
</customField> 
Cuestiones relacionadas