2011-04-06 81 views
2

請告訴我最effcient XSL用於將這個XML:XML到HTML:輸出文本子節點

<matflash label="popup" btn-label="Interview Transcript"> 
    <flashtext><a href="/education/resources/students/video_cases/protected/ece/product/media/508_Guidance_Transcript.doc">Download Transcript</a></flashtext> 
    <flashtext class="H1">Linda Rudolph - Teacher</flashtext> 
    <flashtext class="H1">Little Sprouts, Methuen School</flashtext> 
    <flashtext><b>Interviewer:</b> Linda, could you start by describing to me what you think the basis of a well-managed classroom is. Describe in your own words, what you think the basis of a well managed classroom is? What helps you get there?</flashtext> 
    <flashtext><b>Linda:</b> I think just having a well managed classroom is just having good expectations so that for the children, that they know their limits, what is expected of them, what is just being able to tell them, "Okay, this is what we're doing today.", and then just set it up for them and then they know they can accomplish it, just not having any mixed messages for them.</flashtext> 
    <flashtext><b>Linda:</b> Having a well managed classroom is just having a really good curriculum that the teacher's can follow and teach the children so that they're interested and they know exactly what's expected of them and then the management comes from them just knowing what's expected of them, just setting up classroom rules and everybody being able to follow them and knowing what's expected.</flashtext> 
... 
</matflash> 

這個HTML:

<div id="interview"> 
    <div><a href="/education/resources/students/video_cases/protected/ece/product/media/508_Guidance_Transcript.doc">Download Transcript</a></div> 
    <div class="H1">Linda Rudolph - Teacher</div> 
    <div class="H1">Little Sprouts, Methuen School</div> 
    <div><b>Interviewer:</b> Linda, could you start by describing to me what you think the basis of a well-managed classroom is. Describe in your own words, what you think the basis of a well managed classroom is? What helps you get there?</div> 
    <div><b>Linda:</b> I think just having a well managed classroom is just having good expectations so that for the children, that they know their limits, what is expected of them, what is just being able to tell them, "Okay, this is what we're doing today.", and then just set it up for them and then they know they can accomplish it, just not having any mixed messages for them.</div> 
    <div><b>Linda:</b> Having a well managed classroom is just having a really good curriculum that the teacher's can follow and teach the children so that they're interested and they know exactly what's expected of them and then the management comes from them just knowing what's expected of them, just setting up classroom rules and everybody being able to follow them and knowing what's expected.</div> 
... 
</div> 

我使用<xsl:value-of><xsl:copy>顯示有麻煩文字的子節點(標籤和文本)。

回答

3

以下樣式:

<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="matflash"> 
     <div id="interview"> 
      <xsl:apply-templates /> 
     </div> 
    </xsl:template> 
    <xsl:template match="flashtext"> 
     <div> 
      <xsl:apply-templates select="@*|node()" /> 
     </div> 
    </xsl:template> 
</xsl:stylesheet> 

當應用到你的源文件將產生以下結果:

<div id="interview"> 
    <div> 
     <a href="/education/resources/students/video_cases/protected/ece/product/media/508_Guidance_Transcript.doc">Download Transcript</a> 
    </div> 
    <div class="H1">Linda Rudolph - Teacher</div> 
    <div class="H1">Little Sprouts, Methuen School</div> 
    <div> 
     <b>Interviewer:</b> 
     Linda, could you start by describing to me what you think the basis of 
     a well-managed classroom is. Describe in your own words, what you 
     think the basis of a well managed classroom is? What helps you get 
     there? 
    </div> 
    <div> 
     <b>Linda:</b> 
     I think just having a well managed classroom is just having good 
     expectations so that for the children, that they know their limits, 
     what is expected of them, what is just being able to tell them, "Okay, 
     this is what we're doing today.", and then just set it up for them and 
     then they know they can accomplish it, just not having any mixed 
     messages for them. 
    </div> 
    <div> 
     <b>Linda:</b> 
     Having a well managed classroom is just having a really good 
     curriculum that the teacher's can follow and teach the children so 
     that they're interested and they know exactly what's expected of them 
     and then the management comes from them just knowing what's expected 
     of them, just setting up classroom rules and everybody being able to 
     follow them and knowing what's expected. 
    </div> 
</div> 

注意使用的身份變換通過flashtext節點下的所有元素進行復制。這與您的輸入一起工作,但如果您的元素高於或低於您不想複製的matflashflashtext,則需要進行調整。一如往常,不同的要求會導致不同的解決方案

編輯:經過思考,如果你只是想複製flashtext下的所有內容,並有東西仍然工作更大的文件中,那麼標準的身份轉換模板可以用一個copy-of代替:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="matflash"> 
     <div id="interview"><xsl:apply-templates /></div> 
    </xsl:template> 
    <xsl:template match="flashtext"> 
     <div><xsl:copy-of select="@*|node()" /></div> 
    </xsl:template> 
</xsl:stylesheet> 

...產生相同的輸出。

+0

'select =「@ * | node()'是我不知道該做的事情,謝謝, – 2011-04-06 15:55:23

+0

@ two7s_clash - 不客氣,看到我的編輯,我也縮短了 – 2011-04-06 16:04:33

+0

+1正確答案。 – 2011-04-06 17:02:04