如何根据某个其他后代的值遍历特定节点的后代节点

时间:2017-12-05 09:42:09

标签: xslt xpath xslt-1.0 xslt-2.0

我有一个例子xml如下:

 <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
            <topic>
        <table>...</table>
        <p>...</p>
        <ul>

              <li>there is a tag
        <b>
    <u>HERE an XREF tag </u>
    </b>has to be added after the fn tag that point to the fn id 
    <fn id="id-523">this is a second fn with id-523</fn>
    </li>
      <li>there is another third
    <fn id="id-524">this is a third  fn  id-524</fn>
        </li>
              <li>there is another fourth
 <fn id="id-525">this is a fourth  fn  id-525</fn>
    <xref href="#topic_gtc_yn2_bcb/id-525" format="dita" type="fn"/>
    </li>
              <li>there is a xref  tag that points to the fn id-523 
    <xref href="#topic_gtc_yn2_bcb/id-523" format="dita" type="fn"/>
    </li>
            </ul>
        </topic>

现在我必须检查fn标签内的id属性。 如果对应于那个id,我在同一个ul内部有一个外部参照标记(它也可以是一个表或者ol元素)然后删除id属性,否则我=如果我有一个具有相同id的外部参照标记(这里例如id = 523)。 然后我必须在特定的fn标签之后引入外部参照标记。

预期的输出因此将是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE topic PUBLIC "-//OASIS//DTD DITA Topic//EN" "topic.dtd">
        <topic>
    <table>...</table>
    <p>...</p>
    <ul>

          <li>there is another tag
    <b>
<u>HERE an XREF tag </u>
</b>has to be added after the fn tag that point to the fn id 
       <fn id="id-523">this is a second fn with id-523</fn>
<xref href="#topic_gtc_yn2_bcb/id-523" format="dita" type="fn"/>
    </li>
      <li>there is another third
<fn>this is a third  fn  id-524</fn>
    </li>
          <li>there is another fourth <fn id="id-525">this is a fourth  fn  id-525</fn>
<xref href="#topic_gtc_yn2_bcb/id-525" format="dita" type="fn"/>
</li>
          <li>there is a xref  tag that points to the fn id-523 
<xref href="#topic_gtc_yn2_bcb/id-523" format="dita" type="fn"/>
</li>
        </ul>
    </topic>

1 个答案:

答案 0 :(得分:0)

你可以试试这个

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="fn[@id]">
    <xsl:variable name="id" select="concat('#topic_gtc_yn2_bcb/', @id)"/>
    <xsl:copy>
        <xsl:if test="ancestor::*[matches(name(), '^(ul|table|ol)$')]//xref/@href  = $id">
            <xsl:copy-of select="@*"></xsl:copy-of>
        </xsl:if>
        <xsl:apply-templates/>
    </xsl:copy>
    <xsl:if test="ancestor::*[matches(name(), '^(ul|table|ol)$')]//xref/@href  = $id">
        <xsl:if test="not(following-sibling::*[1][self::xref/@href = $id])">
            <xsl:copy-of select="ancestor::*[matches(name(), '^(ul|table|ol)$')]//xref[@href  = $id]"></xsl:copy-of>
        </xsl:if>
    </xsl:if>
</xsl:template>