簡體   English   中英

如何將XSLT模板應用於父級

[英]How to apply xslt template to parent

我在將html添加到所選節點的父節點時遇到了一些麻煩。 我有一個xml文件:

<component>
    <section>
        <text>
            <content>
              some text 1
            </content>
            <content>
              some text 2
            </content>
        </text> 
    </section>
    <section>
        <text>
            <otherElements></otherElements>
        </text> 
    </section>
    .....
</component>

有很多部分,有些在文本元素下包含內容元素,有些則沒有。 我只將模板應用於具有content元素的模板,並輸出,其中每個text元素一個表,每個content元素一行:

   <table>
      <tbody>
         <tr><td>some text 1</td>
         <tr><td>some text 2</td>
      </tbody>
   </table>

當我選擇內容元素

<xsl:template match="section/text/content">

我能夠向其中添加行,但是我不確定如何向父節點添加表和tbody標簽。 如果我開始

<xsl:template match="section/text">

它還將其他元素與我不想要的元素添加到文本元素中。

使用match="section/text [content]"

這樣的事情應該做(未經演示,只是為了說明這個想法):

<xsl:template match="/">
    <xsl:apply-templates select="//component/section[text/content]"/>
</xsl:template>

<xsl:template match="section[text/content]">
    <table><tbody>
        <xsl:apply-templates select="text/content"/>
    </tbody></table>
</xsl:template>

<xsl:template match="content">
    <tr><td><xsl:value-of select="text()"/></td></tr>
</xsl:template>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM