2012-04-09 115 views
3

我想選擇所有節點<cci:p>不要有一個屬性。XSLT - 選擇節點沒有屬性

所以,在下面的例子中,有一個節點(用粗體文本或**表示)引起了一些問題。基本上,我想選擇所有<cci:p>節點並將它們輸出包裝在<p>標籤中。但是這個節點會導致輸出一個額外的段落,這是不正確的。在這種情況下,我想要發生的是,如果找到具有該屬性的節點,我想將它附加到先前處理的節點。

這裏就是我得到:

加州奧克蘭前學生涉嫌在加州的一個小基督教 大學開槍,打死7人,打傷3的,是針對學校 管理員昨天警方說他曾感到不公平地對待他的前同學, 。

奧克蘭警察局長霍華德·喬丹在新聞發佈會上說,一吳作棟,43,誰 已經從OIKOS大學開除,一直合作與調查

被羈押後,但也不是特別懊悔。

我們知道他來到這裏是爲了找到一位管理員,她不在這裏,她是 喬丹說。然後他通過整個建築系統和 隨機槍擊案的受害者去

下面是我想得:。

加州奧克蘭前學生涉嫌開槍的在加利福尼亞州的一個小型基督教徒 學院裏,造成7人死亡,3人受傷,目標是學校管理人員和他認爲曾經不公平對待他的前同學的學校 ,昨天警方稱 。

奧克蘭警察局長霍華德·喬丹在新聞發佈會上說,一吳作棟,43,誰 已經從OIKOS大學開除,一直合作與調查 之後被監禁,但不是特別懊悔。

我們知道他來到這裏的目的是找到一位管理員,她不是在這裏,她是 喬丹說。然後他通過整個建築系統和 隨機槍擊案的受害者去

示例XML:

<cci:body class="element" displayname="body" name="body"> 
    <cci:p>OAKLAND, Calif. — A former student suspected of opening fire at a small Christian college in California, killing seven people and wounding three, was targeting a school administrator and former classmates who he felt had treated him unfairly, police said yesterday.</cci:p> 
    <cci:p>Oakland Police Chief Howard Jordan said at a news conference that One Goh, 43, who had been expelled from Oikos University, had been cooperative with investigators </cci:p> 
    **<cci:p ccix:annotation="insertion">after being taken into custody but 「not particularly remorseful.」</cci:p>** 
    <cci:p>「We know that he came here with the intent of locating an administrator, and she was not here,」 Jordan said. 「He then went through the entire building systematically and randomly shooting victims.」</cci:p> 
    <cci:p>The midmorning attack at Oikos, a small Oakland college that has links to the Korean-American Christian community, was the deadliest shooting rampage on a U.S. college campus since </cci:p> 
</cci:body> 

樣品XSLT:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cci="urn:schemas-ccieurope.com" 
    xmlns:ccit="http://www.ccieurope.com/xmlns/ccimltables" xmlns:ccix="http://www.ccieurope.com/xmlns/ccimlextensions" 
    exclude-result-prefixes="xsl cci ccit ccix"> 

    <xsl:strip-space elements="*" /> 
    <xsl:output method="html" encoding="UTF-8" /> 

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

    <xsl:template match="cci:p"> 
     <xsl:choose> 
      <xsl:when test="@ccix:annotation='insertion'"> 
       <xsl:apply-templates /> 
      </xsl:when> 

      <xsl:otherwise> 
       <p> 
        <xsl:apply-templates /> 
       </p> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 

    <xsl:template match="cci:italic"> 
     <em> 
      <xsl:apply-templates /> 
     </em> 
    </xsl:template> 

    <xsl:template match="cci:endnote_contrib"> 
     <em> 
      <xsl:apply-templates /> 
     </em> 
    </xsl:template> 

    <xsl:template match="cci:extra_leading"> 
    </xsl:template> 

    <xsl:template match="cci:bold"> 
     <strong> 
      <xsl:apply-templates /> 
     </strong> 
    </xsl:template> 

    <xsl:template match="cci:subhead"> 
     <h2 class="cci-subhead"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="ccit:table"> 
     <table class="cci-table"> 
      <xsl:apply-templates /> 
     </table> 
    </xsl:template> 

    <xsl:template match="ccit:tr"> 
     <tr> 
      <xsl:apply-templates /> 
     </tr> 
    </xsl:template> 

    <xsl:template match="ccit:td"> 
     <td> 
      <xsl:value-of select="." /> 
     </td> 
    </xsl:template> 

    <xsl:template match="cci:l_category"> 
     <h2 class="cci-category"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_category_sub"> 
     <h2 class="cci-category-sub"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_region"> 
     <h2 class="cci-region"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_region_location"> 
     <h2 class="cci-region-location"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_region_sub"> 
     <h2 class="cci-region-sub"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="factbox_bold"> 
     <strong> 
      <xsl:apply-templates /> 
     </strong> 
    </xsl:template> 

    <xsl:template match="cci:factbox_head"> 
     <strong> 
      <xsl:value-of select="." /> 
     </strong> 
    </xsl:template> 

    <xsl:template match="cci:z_sym_round_bullet"> 
     &#8226; 
     <xsl:value-of select="." /> 
    </xsl:template> 

    <xsl:template match="cci:z_sym_triangle_bullet"> 
     &#8226; 
     <xsl:value-of select="." /> 
    </xsl:template> 

    <xsl:template match="text()"> 
     <xsl:value-of select="." /> 
    </xsl:template> 
</xsl:stylesheet> 

回答

3

我會定義一個鍵將具有該屬性的元素映射到要插入的前一個兄弟:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cci="urn:schemas-ccieurope.com" 
    xmlns:ccit="http://www.ccieurope.com/xmlns/ccimltables" xmlns:ccix="http://www.ccieurope.com/xmlns/ccimlextensions" 
    exclude-result-prefixes="xsl cci ccit ccix"> 

    <xsl:key name="k1" match="cci:p[@ccix:annotation = 'insertion']" 
     use="generate-id(preceding-sibling::cci:p[not(@ccix:annotation)][1])"/> 

    <xsl:strip-space elements="*" /> 
    <xsl:output method="html" encoding="UTF-8" /> 

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

    <xsl:template match="cci:p[not(@ccix:annotation)]"> 
     <p> 
     <xsl:apply-templates select="node() | key('k1', generate-id())/node()"/> 
     </p> 
    </xsl:template> 

    <xsl:template match="cci:p[@ccix:annotation = 'insertion']"/> 

    <xsl:template match="cci:italic"> 
     <em> 
      <xsl:apply-templates /> 
     </em> 
    </xsl:template> 

    <xsl:template match="cci:endnote_contrib"> 
     <em> 
      <xsl:apply-templates /> 
     </em> 
    </xsl:template> 

    <xsl:template match="cci:extra_leading"> 
    </xsl:template> 

    <xsl:template match="cci:bold"> 
     <strong> 
      <xsl:apply-templates /> 
     </strong> 
    </xsl:template> 

    <xsl:template match="cci:subhead"> 
     <h2 class="cci-subhead"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="ccit:table"> 
     <table class="cci-table"> 
      <xsl:apply-templates /> 
     </table> 
    </xsl:template> 

    <xsl:template match="ccit:tr"> 
     <tr> 
      <xsl:apply-templates /> 
     </tr> 
    </xsl:template> 

    <xsl:template match="ccit:td"> 
     <td> 
      <xsl:value-of select="." /> 
     </td> 
    </xsl:template> 

    <xsl:template match="cci:l_category"> 
     <h2 class="cci-category"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_category_sub"> 
     <h2 class="cci-category-sub"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_region"> 
     <h2 class="cci-region"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_region_location"> 
     <h2 class="cci-region-location"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="cci:l_region_sub"> 
     <h2 class="cci-region-sub"> 
      <xsl:value-of select="." /> 
     </h2> 
    </xsl:template> 

    <xsl:template match="factbox_bold"> 
     <strong> 
      <xsl:apply-templates /> 
     </strong> 
    </xsl:template> 

    <xsl:template match="cci:factbox_head"> 
     <strong> 
      <xsl:value-of select="." /> 
     </strong> 
    </xsl:template> 

    <xsl:template match="cci:z_sym_round_bullet"> 
     &#8226; 
     <xsl:value-of select="." /> 
    </xsl:template> 

    <xsl:template match="cci:z_sym_triangle_bullet"> 
     &#8226; 
     <xsl:value-of select="." /> 
    </xsl:template> 

    <xsl:template match="text()"> 
     <xsl:value-of select="." /> 
    </xsl:template> 
</xsl:stylesheet> 

使用該樣式表Saxon 6.5。5個輸出結果

<html> 
    <p>OAKLAND, Calif. &#8212; A former student suspected of opening fire at a small Christian college in California, killing seven people 
     and wounding three, was targeting a school administrator and former classmates who he felt had treated him unfairly, police 
     said yesterday. 
    </p> 
    <p>Oakland Police Chief Howard Jordan said at a news conference that One Goh, 43, who had been expelled from Oikos University, 
     had been cooperative with investigators after being taken into custody but &#8220;not particularly remorseful.&#8221; 
    </p> 
    <p>&#8220;We know that he came here with the intent of locating an administrator, and she was not here,&#8221; Jordan said. &#8220;He then went 
     through the entire building systematically and randomly shooting victims.&#8221; 
    </p> 
    <p>The midmorning attack at Oikos, a small Oakland college that has links to the Korean-American Christian community, was the 
     deadliest shooting rampage on a U.S. college campus since 
    </p> 
</html> 

爲輸入

<cci:body class="element" displayname="body" name="body" xmlns:cci="urn:schemas-ccieurope.com" xmlns:ccix="http://www.ccieurope.com/xmlns/ccimlextensions"> 
    <cci:p>OAKLAND, Calif. — A former student suspected of opening fire at a small Christian college in California, killing seven people and wounding three, was targeting a school administrator and former classmates who he felt had treated him unfairly, police said yesterday.</cci:p> 
    <cci:p>Oakland Police Chief Howard Jordan said at a news conference that One Goh, 43, who had been expelled from Oikos University, had been cooperative with investigators </cci:p> 
    <cci:p ccix:annotation="insertion">after being taken into custody but 「not particularly remorseful.」</cci:p> 
    <cci:p>「We know that he came here with the intent of locating an administrator, and she was not here,」 Jordan said. 「He then went through the entire building systematically and randomly shooting victims.」</cci:p> 
    <cci:p>The midmorning attack at Oikos, a small Oakland college that has links to the Korean-American Christian community, was the deadliest shooting rampage on a U.S. college campus since </cci:p> 
</cci:body> 
+0

太感謝你了!這是有效的,並且完全符合我想要的。 – liltitus27 2012-04-10 20:12:12