2017-12-05 3 views
0

Ich habe ein XML-Beispiel wie folgt:Wie man einen bestimmten Knoten Nachkomme Knoten zu durchqueren, basierend auf einigen anderen Abkömmling Wert

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

Hier muss ich die ID überprüfen Attribut innerhalb des fn Tag vorhanden. Wenn ich dieser ID folge, habe ich ein xref -Tag innerhalb derselben ul (das kann auch für eine Tabelle oder ein ol-Element sein) dann entferne das ID-Attribut else I = wenn ich ein xref-Tag mit derselben ID habe (hier für zB id = 523). Dann muss ich Xref-Tag direkt nach diesem bestimmten Fn-Tag einführen.

Erwartete Ausgabe wird daher:

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

Als Ausgangspunkt, ich versuche etwas wie unten zu verwenden: geek1209

+0

Also haben Sie 'fn' Elemente in der XML-Eingabe oder einfach einen Text wie' < fn'? –

+0

Ich muss es auf Tag anwenden – geek1209

Antwort

0

Sie können diese versuchen

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