xml - XSLT - 选择没有属性的节点

标签 xml xslt logic

我要选择所有节点<cci:p> 有属性。

因此,在下面的示例中,有一个节点(用粗体文本或 ** 表示)导致了一些问题。基本上,我想选择所有 <cci:p>节点并将它们包裹在 <p> 中输出标签。但是这个节点导致输出了一个额外的段落,这是不正确的。在这种情况下,我希望发生的是,如果找到具有该属性的节点,我想将其附加到先前处理的节点。

这是我得到的:

加利福尼亚州奥克兰 ���� 一名前学生涉嫌向一名小基督徒开火 加利福尼亚州的一所大学,造成七人死亡,三人受伤,目标是一所学校 警方称,他认为管理员和以前的同学对他不公平 昨天。

奥克兰警察局长霍华德乔丹在新闻发布会上说,43 岁的 One Goh 曾被 Oikos 大学开除,曾与调查人员合作

在被拘留后,但...并不是特别后悔。

����我们知道他来这里的目的是寻找管理员,而她是 不在这里,”乔丹说。 ����然后他系统地检查了整个建筑物并 随机射击受害者。

这是我想要得到的:

加利福尼亚州奥克兰 ���� 一名前学生涉嫌向一名小基督徒开枪 加利福尼亚州的一所大学,造成七人死亡,三人受伤,目标是一所学校 警方称,他认为管理员和以前的同学对他不公平 昨天。

奥克兰警察局长霍华德乔丹在新闻发布会上说,43 岁的 One Goh 曾被 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>

最佳答案

我会定义一个键来将具有该属性的元素映射到您要插入它们的前一个兄弟:

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

关于xml - XSLT - 选择没有属性的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076331/

相关文章:

java - 计算进行时显示进度条

ANT 场景条件依赖目标

java - 在 JAXB 中,如何将节点值设置为字符串字段的属性值?

xslt - 当某些日期不在源 xml 中时,如何输出一系列日期的数据

java - SAX错误: incompatible types: String cannot be converted to InputSource

javascript - 将 XSL 字符串传递给 Javascript

php - 需要有关音乐网站逻辑/显示问题的建议

xml - xslt/xpath 中的跟随或结束?

java - 如何在 Java EE 中使用 XmlRepresentation?

java - 从 XMLResponse 创建 FileReader?