XML в HTML: вывод буквенных дочерних узлов - PullRequest
2 голосов
/ 06 апреля 2011

Какой самый эффективный XSL для преобразования этого XML:

<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> для отображениялитеральные дочерние узлы (тег и текст) flashtext.

1 Ответ

3 голосов
/ 06 апреля 2011

Следующая таблица стилей:

<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. Это работает с вашим вводом, но его необходимо настроить, если у вас есть элементы выше или ниже matflash и flashtext, через которые вы не хотите копировать. Как всегда, разные требования приводят к разным решениям.

Редактировать: После размышления, если вы просто хотите скопировать все, что ниже flashtext, и иметь что-то, что все еще работает в большом документе, тогда стандартный шаблон преобразования идентификаторов можно заменить одним copy-of * * 1016

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

... который выдает тот же результат.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...