使用XSLT迭代和链接XML源代码中的元素

时间:2013-07-10 06:24:11

标签: xml xslt

两个问题:

  1. 如何基于完美匹配的ID将第一个与对应的相互链接? XML源示例:

      

    < 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. 如何基于与@corresp建立的链接链接来自两个或多个div的segs? XML源示例:

      

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

          
        
      
  3. 感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是一个分组问题,使用Muenchian分组http://www.jenitennison.com/xslt/grouping/muenchian.xml解决它的XSLT 1.0;那样的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>

转换输入

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

进入输出

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