XSL1.0:如何在特定條件下只解析標記的文本內容

[英]XSL1.0: How to parse only text content of a tag under a certain condition


i have a simple problem i cannot solve. I have the following xml and xsl. I need to print out the SPAN and P tag including their content when they are not inside a "title" tag. In this case i just want to print out their content. This is an extract of my code. Right now i always print out both tag and content and cannot achieve what i want. This is what i'm printing:

我有一個簡單的問題我不能解決。我有以下xml和xsl。我需要打印SPAN和P標簽,包括它們的內容,當它們不在“title”標簽中時。在這種情況下,我只想打印出他們的內容。這是我的代碼摘錄。現在我總是打印標簽和內容,不能實現我想要的。這是我正在打印的:

<?xml version="1.0" encoding="utf-8"?>
<article class="story-ipad sortir">
    <title>
    <p>Il était une fois
        <span>à jamais</span>
    </p>
    <title>
    <text>
        <p>Resequam, necullitae exerio quis por si dolecature et mincta pro. <br/>
        </p>
        <span>La Belle et la Bête,</span>
    </text>
</article>

And this is what i would like:

這就是我想要的:

<?xml version="1.0" encoding="utf-8"?>
<article class="story-ipad sortir">
    <title>
    Il était une fois à jamais
    </title>
    <text>
        <p>Resequam, necullitae exerio quis por si dolecature et mincta pro. <br/>
        </p>
        <span>La Belle et la Bête,</span>
    </tex

t>

t >

I cannot modify the code too much, what i want to do is in my last template that match many html tag add an exception to have different if the tag is analyzing is //title/span ecc....otherwise to have another templat i call just from within "tiltle" but only for span and p tags, cause all other tags must behave differently.Can you help me?

我不能修改代碼太多,我想要做的是在我最后的模板匹配許多有不同的html標記添加一個例外如果標記分析/ /標題/跨度ecc ....否則,為了有另一個templat,我只從“tiltle”中調用,而只針對span和p標記,因為所有其他標記的行為必須不同。你能幫我嗎?

<xsl:template match="span|div|font|
        tt|i|b|big|small|u|s|strike|
        em|strong|q|sub|sup|cite|abbr|acronym|
        hr|blockquote|center|
        img|
        table|col|colgroup|thead|tfoot|tbody|tr|p|
        th|td|summary" name="html-noclass-t">

A CONDITION HERE TO FIND OUT IF THE TAG IS UNDER A CERTAIN PATH FOR EXAMPLE

這里的一個條件,用於查找標記是否位於某個路徑下


<?xml version="1.0" encoding="utf-8"?>
<doc xml:lang="fr">
    <article>
        <titles id="U1402860665587eXC">
            <title id="U1402864848468jy">
                <p id="U1402864848468C8D">Il était une fois <br/>
                    <span id="U1402864848468aP">à jamais</span>
                </p>
                </br>
            </title>
        </titles>
        <texte id="U14028606655875nG">
            <p>Resequam, necullitae exerio quis por si dolecature et mincta pro. <br/>
            </p>
            <span id="U1402864848468liD">La Belle et la Bête,</span>
        </texte>
    </article>
</doc>

With an XSL:

與一個XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="no"/>


    <xsl:template match="/">
        <xsl:apply-templates select="/doc/article" />
    </xsl:template>

    <xsl:template match="/doc/article">
        <article class="story-ipad sortir">
            <xsl:call-template name="header"/>
            <xsl:call-template name="text"/>
        </article>
    </xsl:template>

    <xsl:template name="header">
        <xsl:choose>
            <xsl:when test="//article/titles/title">
                <xsl:apply-templates select="//article/titles/title"/>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="text">
        <xsl:choose>
            <xsl:when test="//text">
                <xsl:apply-templates select="//text"/>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </xsl:template>

    <xsl:template match="span|div|font|
    tt|i|b|big|small|u|s|strike|
    em|strong|q|sub|sup|cite|abbr|acronym|
    hr|blockquote|center|
    img|
    table|col|colgroup|thead|tfoot|tbody|tr|p|
    th|td|summary" name="html-noclass-t">
        <xsl:element name="{name()}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

2 个解决方案

#1


2  

It sounds like what you're looking for is XSLT modes.

這聽起來像是XSLT模式。

#2


2  

I'm wondering whether you're overcomplicating things. If you want a <title> element with text-only content, then write the title element and copy the text content directly without calling or applying templates. If you want special behavior for some descendants of a <title> element, than you could write different templates with different matches, e.g. like so:

我想知道你是不是把事情弄得太復雜了。如果您想要一個帶有純文本內容的元素,那么可以直接編寫標題元素並直接復制文本內容,而不需要調用或應用模板。如果您想為元素的某些后代創建特殊的行為,那么您可以使用不同的匹配編寫不同的模板,例如:</p>

I'd suggest trying whether something like this does what you want to do:

我建議你試試這樣的東西是否能達到你想要的效果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="utf-8" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="/doc/article" />
  </xsl:template>

  <xsl:template match="/doc/article">
    <article class="story-ipad sortir">
      <title>
        <xsl:apply-templates select="titles/title"/>
      </title>
      <text>
        <xsl:apply-templates select="texte/*"/>
      </text>
    </article>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="title//*[self::p or self::span or self::br]">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

</xsl:stylesheet>

When applied to your source document (which I modified because it has a closing </br> without a matching opening one):

當應用到您的源文檔時(我修改了它,因為它有一個關閉
,沒有匹配的開頭):

<?xml version="1.0" encoding="utf-8"?>
<article class="story-ipad sortir">
  <title>Il était une fois à jamais</title>
  <text>
    <p>Resequam, necullitae exerio quis por si dolecature et mincta pro. <br/>
    </p>
    <span>La Belle et la Bête,</span>
  </text>
</article>

This comes out:

這出來:

<?xml version="1.0" encoding="utf-8"?>
<article class="story-ipad sortir">
  <title>
    <title>
      Il était une fois 
      à jamais


    </title>
  </title>
  <text>
    <p>Resequam, necullitae exerio quis por si dolecature et mincta pro. <br/>
    </p>
    <span>La Belle et la Bête,</span>
  </text>
</article>

There's another thing I noticed: You're using structures like

我還注意到另一件事:你在使用像這樣的結構

<xsl:choose>
    <xsl:when test="//article/titles/title">
        <xsl:apply-templates select="//article/titles/title"/>
    </xsl:when>
    <xsl:otherwise/>
</xsl:choose>

This is equivalent to this one line:

這就等於這一行:

<xsl:apply-templates select="//article/titles/title"/>

The empty <xsl:otherwise> can be omitted in any case, which leaves you with a single case. When testing for a single case, <xsl:if> is nicer. But in general, <xsl:if> and <xsl:choose> are over-used because in many cases, <xsl:apply-templates> will do the job and is considered better practice in XSLT.

空的 在任何情況下都可以被忽略,這樣就只剩下一個case了。當測試單個案例時, 更好。但是一般來說, 被過度使用,因為在許多情況下, 將完成這項工作,在XSLT中被認為是更好的實踐。

What you do is redundant double checking: You first check "is there something to supply to <xsl:apply-templates>?", and if there is nothing to supply, you do nothing. However, when supplied with nothing, <xsl:apply-templates> will do nothing anyways. So, no checking required. If there are no titles, <xsl:apply-templates> won't do anything, and if there are titles, you'd be calling it anyways.

您要做的是重復檢查:首先檢查“是否有什么東西可以提供給 ?”如果沒有東西可以供應,你就什么也不做。但是,當不提供任何內容時, 無論如何都不起作用。所以,不需要檢查。如果沒有標題, 不會做任何事情,如果有標題,無論如何您都會調用它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/01/14/72f1a1a4eeccc8028d413ba29097afdc.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com