XSLT

2011-02-08 18 views
5

का उपयोग कर प्रत्येक तालिका के लिए पहली HTML तालिका पंक्ति को शीर्ष पंक्ति में कनवर्ट करें मेरे पास मेरे XML के अंदर कुछ HTML सामग्री है। सामग्री को सही क्षेत्र में खींचने के लिए पहले मैं केवल <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> का उपयोग कर सकता था। thead/tr/th के सेट में प्रत्येक तालिका के <tbody> के अंदर पहले <tr> को परिवर्तित करने की एक नई आवश्यकता है।XSLT

मैं कैसे कन्वर्ट करने के लिए पर उलझन में हूँ, वास्तव में भी किनारे नहीं कहां से शुरू करें:

...

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

में:

... 
<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। सबसे मौलिक और शक्तिशाली एक्सएसएलटी डिजाइन पैटर्न - अतिव्यापी पहचान नियम का उपयोग करके, पूर्ण और संक्षिप्त समाधान के लिए मेरा उत्तर देखें। –

उत्तर

1

मैं कुछ html है मेरे एक्सएमएल के अंदर सामग्री। इससे पहले मैं क्षेत्र में सामग्री को खींचने के लिए बस <xsl:copy-of select="customFields/customField[@name='mainContent']/html"/> का उपयोग कर सकता था। एक नई आवश्यकता के अंदर पहले <tr> कन्वर्ट करने के लिए है हर तालिका के <tbody>thead/tr/th के एक सेट में।

यह परिवर्तन:

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

जब प्रदान की 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> 

वास्तव में चाहता था, सही परिणाम का उत्पादन

Do ध्यान दें:

"ओवरराइड पहचान नियम" डिजाइन पैटर्न प्रयोग किया जाता है। यह सबसे मौलिक और शक्तिशाली एक्सएसएलटी डिजाइन पैटर्न है।

अद्यतन:

Flynn1179 द्वारा देखा के रूप में, समस्या (ऊपर) के ओ पी की परिभाषा उत्पादन वह प्रदान करता है के रूप में परिणाम चाहते थे के साथ असंगत है। इस आउटपुट में न केवल trtbody के अंदर thead/tr (और td बच्चों को th) में परिवर्तित किया गया है, लेकिन theadtbody के बाहर स्थानांतरित हो गया है।

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

जब एक ही 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"> 
     <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। सही और पूर्ण – Flack

+0

+1 बेहतर उत्तर। –

+0

@Alejandro और @Flack: धन्यवाद। हां, यह समाधान पूरी तरह से "धक्का" शैली है, जबकि दूसरा समाधान खींचने की शैली है ... एक और विषय में, मुझे लगता है कि आप मेरी नवीनतम ब्लॉग पोस्ट को पढ़ने में रुचि रखते हैं: "द बाइनरी सर्च ट्री डेटा स्ट्रक्चर-मज़े करना XPath 3.0 "पर: http://dnovatchev.wordpress.com/2011/02/08/the-binary-search-tree-data-structurehaving-fun-with-xpath-3-0/ –

0

मामले में यह है कि क्या ओ पी, चाहता है यहाँ इस मामले के लिए भी समाधान संशोधित किया गया है वास्तव में है

इसे आज़माएं:

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

यह बस प्रतिस्थापित करता है आपकी मौजूदा tbody तत्व thead वाली पहली पंक्ति वाली है, और tbody जिसमें पहले सभी शामिल हैं, और फिर tr में th के साथ सभी td तत्वों को प्रतिस्थापित करता है।

+0

धन्यवाद कि मैं आउटपुट के लिए कैसे पूछता हूं, इस पर लेने के लिए Flynn1179 धन्यवाद। – ConfusedMuch

0
बस मस्ती के लिए

, इस स्टाइलशीट:

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

आउटपुट:

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