2011-02-08 34 views
5

Tôi có một số nội dung html bên trong XML của mình. Trước đây tôi chỉ có thể sử dụng <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> để kéo nội dung vào đúng khu vực. Yêu cầu mới là chuyển đổi <tr> đầu tiên bên trong mỗi bảng <tbody> thành một bộ thead/tr/th.Chuyển đổi hàng của bảng HTML đầu tiên thành hàng tiêu đề cho mỗi bảng bằng cách sử dụng XSLT

Tôi đang bối rối về cách chuyển đổi, trên thực tế thậm chí không bờ bắt đầu từ đâu:

...

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

thành:

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

Tốt câu hỏi, 1. Xem câu trả lời của tôi cho một giải pháp hoàn chỉnh và ngắn gọn, bằng cách sử dụng mẫu thiết kế XSLT cơ bản và mạnh mẽ nhất - quy tắc nhận dạng quá mức. –

Trả lời

1

Tôi có một số html nội dung bên trong của tôi XML. Trước đây tôi chỉ có thể sử dụng <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> để kéo nội dung vào đúng khu vực . Yêu cầu mới là chuyển đổi <tr> đầu tiên bên trong mỗi bảng <tbody> vào một bộ thead/tr/th.

chuyển đổi này:

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

khi áp dụng trên tài liệu XML cung cấp:

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

sản xuất chính xác mong muốn, đúng kết quả:

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

Đỗ lưu ý:

Những "quy tắc sắc overriden" mẫu thiết kế được sử dụng. Đây là mẫu thiết kế XSLT cơ bản và mạnh mẽ nhất.

CẬP NHẬT:

Như chú ý của Flynn1179, định nghĩa của OP của vấn đề (ở trên) là không phù hợp với sản lượng cung cấp ông như muốn kết quả. Trong đầu ra này không chỉ là tr đầu tiên bên trong của tbody được chuyển đổi thành thead/tr (và td trẻ em thành th), nhưng thead được di chuyển ra ngoài tbody.

Trong trường hợp này thực sự là những gì các OP muốn, đây được sửa đổi Giải pháp này cũng cho trường hợp này:

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

khi áp dụng trên tài liệu XML tương tự, kết quả là:

<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. Chính xác và đầy đủ. – Flack

+0

+1 Câu trả lời hay hơn. –

+0

@Alejandro và @Flack: Cảm ơn bạn. Có, giải pháp này là hoàn toàn "đẩy" phong cách, trong khi các giải pháp khác là kéo phong cách ... Trong một chủ đề khác, tôi nghĩ rằng bạn muốn được quan tâm để đọc bài đăng blog mới nhất của tôi: "Cơ cấu dữ liệu cây tìm kiếm nhị phân-có vui vẻ với XPath 3.0 "tại: http://dnovatchev.wordpress.com/2011/02/08/the-binary-search-tree-data-structurehaving-fun-with-xpath-3-0/ –

0

Hãy thử cách này:

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

Nó chỉ đơn giản là thay thế Phần tử tbody hiện tại của bạn có một thead chứa hàng đầu tiên và tbody chứa tất cả trừ giá trị đầu tiên, sau đó thay thế tất cả các phần tử td trong tr đầu tiên bằng th thay thế.

+0

Cảm ơn Flynn1179 vì đã chọn cách tôi yêu cầu đầu ra. – ConfusedMuch

0

Chỉ cần cho vui, kiểu này:

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

Output:

<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> 
Các vấn đề liên quan