2011-02-08 13 views
5

Mam zawartość HTML w moim XML. Wcześniej mogłem po prostu użyć <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/>, aby przenieść zawartość do właściwego obszaru. Nowym wymogiem jest zamiana pierwszego <tr> w każdym z tabel <tbody> w zestaw thead/tr/th.Konwersja pierwszego wiersza tabeli HTML do wiersza nagłówka dla każdej tabeli przy użyciu XSLT

jestem mylić w jaki sposób przekonwertować, w rzeczywistości nawet nie brzeg czego zacząć:

...

<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> 
... 

do:

... 
<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

Dobre pytanie, +1. Zobacz moją odpowiedź na kompletne i krótkie rozwiązanie, korzystając z najbardziej podstawowego i potężnego wzorca projektowego XSLT - nadrzędnej reguły tożsamości. –

Odpowiedz

1

Mam html treść w moim pliku XML: . Poprzednio mogłem po prostu użyć <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> , aby wyciągnąć zawartość do właściwego obszaru . Nowym wymaganiem jest przekonwertowanie pierwszego <tr> wewnątrz każdego zestawu tabel: <tbody> na zestaw thead/tr/th.

Ta przemiana:

<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> 

po naniesieniu na dostarczonym dokumencie XML:

<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> 

produkuje dokładnie Wanted, poprawny wynik:

<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 uwaga:

"zasada tożsamości nadpisane" wzornictwo jest używany. Jest to najbardziej podstawowy i potężny wzorzec projektowania XSLT.

UPDATE:

Jak zauważyli Flynn1179, definicja PO za problemu (powyżej) jest niezgodna z wyjściem on stanowi, co chciał wynik. W tym wyjściu jest nie tylko pierwszy tr wewnątrz tbody przekształcony na thead/tr (i jego td dzieci na th), ale thead jest przenoszony poza tbody.

W przypadku, jest to naprawdę to, co PO chce, tutaj jest modyfikowany rozwiązanie również w tym przypadku:

<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> 

po naniesieniu na tym samym dokumencie XML, wynik jest:

<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. Prawidłowe i pełne. – Flack

+0

+1 Lepsza odpowiedź. –

+0

@Alejandro i @Flack: Dziękuję. Tak, to rozwiązanie jest całkowicie "pushowe", a inne rozwiązanie to styl ciągnięcia ... W innym temacie, myślę, że byłbyś zainteresowany przeczytaniem mojego ostatniego posta na blogu: "Struktura danych drzewa wyszukiwania binarnego - dobrze się bawiąc z XPath 3.0 "na: http://dnovatchev.wordpress.com/2011/02/08/the-binary-search-tree-data-structurehaving-fun-with-xpath-3-0/ –

0

Wypróbuj:

<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> 

Po prostu zastępuje s Twój istniejący element tbody z thead zawierającym pierwszy wiersz i tbody zawierający wszystkie oprócz pierwszego, a następnie zastępuje wszystkie elementy td w pierwszym tr z th zamiast tego.

+0

Dzięki Flynn1179 za odebranie odpowiedzi na pytanie o wynik. – ConfusedMuch

0

Tylko dla zabawy, arkusz stylów:

<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> 

wyjściowa:

<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> 
Powiązane problemy