【问题标题】:XML to HTML: outputting literal child nodesXML 到 HTML:输出文字子节点
【发布时间】:2011-07-30 22:54:36
【问题描述】:

转换此 XML 最有效的 XSL 是什么:

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

我在使用 &lt;xsl:value-of&gt;&lt;xsl:copy&gt; 显示 flashtext 的字面子节点(标签和文本)时遇到问题。

【问题讨论】:

    标签: html xml xslt xpath transform


    【解决方案1】:

    以下样式表:

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

    ...产生相同的输出。

    【讨论】:

    • select="@*|node() 是我不知道该怎么做。谢谢。
    • @two7s_clash - 不客气。也请参阅我的编辑。我已经把它缩短了很多。
    • 哈哈。顺便说一句,&lt;matflash&gt;'. Where would I move those ` 标签的多个实例上方和下方有很多标记,以便在我的结果中上下颠倒这些东西?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多