gabrielemucho
gabrielemucho

Reputation: 25

Iterate and link across elements in XML source with XSLT

Two questions:

  1. How to link each from the first with the corresponding from the other based on their perfectly matching ids? Example of XML source:

    <DIV>

    <div id="fr">

    <seg id="fr_1">abc</seg>
    <seg id="fr_2">def</seg>
    <seg id="fr_3">ghi</seg>
    <seg id="fr_4">jkl</seg>
    <seg id="fr_5">mno</seg>
    </div>
    <div id="en">
    <seg id="en_1">AAA</seg>
    <seg id="en_2">BBB</seg>
    <seg id="en_3">CCC</seg>
    <seg id="en_4">DDD</seg>
    <seg id="en_5">EEE</seg>
    </div>
    </DIV>

  2. How to link segs from across two or more divs based on linking established with @corresp? Example of XML source:

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <DIV>

    <div id="fr">

    <seg id="fr_1" corresp="#en_1">abc</seg>
    <seg id="fr_2" corresp="#en_2 #en3">def</seg>
    <seg id="fr_3" corresp="#en_3 #en_4">ghi</seg>
    <seg id="fr_4" corresp="#en_4 #en_5">jkl</seg>
    <seg id="fr_5" corresp="#en_6">mno</seg>
    </div>
    <div id="en">
    <seg id="en_1" corresp="#fr_1">ab</seg>
    <seg id="en_2" corresp="#fr_1 #fr_2">cde</seg>
    <seg id="en_3" corresp="#fr_2 #fr_3">fg</seg>
    <seg id="en_4" corresp="#fr_3 fr_4">hij</seg>
    <seg id="en_5" corresp="#fr_4">kl</seg>
    <seg id="en_6" corresp="#fr_5">mno</seg>
    </div>
    </DIV>

Thanks for help.

Upvotes: 0

Views: 97

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

It is a grouping problem, with XSLT 1.0 you solve it using Muenchian grouping http://www.jenitennison.com/xslt/grouping/muenchian.xml; that way the XSLT

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

<xsl:output method="html" indent="yes"/>

<xsl:key name="id" match="seg" use="substring-after(@id, '_')"/>

<xsl:template match="DIV">
  <xsl:copy>
    <ol>
      <xsl:apply-templates select="div/seg[generate-id() = generate-id(key('id', substring-after(@id, '_'))[1])]"/>
    </ol>
  </xsl:copy>
</xsl:template>

<xsl:template match="seg">
  <li>
    <xsl:apply-templates select="key('id', substring-after(@id, '_'))" mode="list"/>
  </li>
</xsl:template>

<xsl:template match="seg" mode="list">
  <xsl:if test="position() > 1">, </xsl:if>
  <xsl:value-of select="."/>
</xsl:template>

</xsl:stylesheet>

transforms the input

<DIV>

    <div id="fr">

        <seg id="fr_1">abc</seg>
        <seg id="fr_2">def</seg>
        <seg id="fr_3">ghi</seg>
        <seg id="fr_4">jkl</seg>
        <seg id="fr_5">mno</seg>
        </div>
        <div id="en">
        <seg id="en_1">AAA</seg>
        <seg id="en_2">BBB</seg>
        <seg id="en_3">CCC</seg>
        <seg id="en_4">DDD</seg>
        <seg id="en_5">EEE</seg>
        </div>
        </DIV>

into the output

<DIV>
   <ol>
      <li>abc, AAA</li>
      <li>def, BBB</li>
      <li>ghi, CCC</li>
      <li>jkl, DDD</li>
      <li>mno, EEE</li>
   </ol>
</DIV>

Upvotes: 0

Related Questions