XSLT任务:使用异常复制不同列表的每个元素的元素列表

时间:2011-07-18 08:02:20

标签: xml xslt xpath

我在尝试设置模板以解决这种情况时遇到了一些麻烦:

我的xml看起来像:

<root>
     <recordset name="companies">
               <record>
                    <id>1</id>
                    <description>Company 1</description>
               </record>
               <record><id>2</id><description>Company 2</description></record>
               ...
               <record><id>n</id><description>Company n</description></record>
     </recordset>
     <gruppi>
          <supplier>
               <agreement>1</agreement>
               <company>
                    <id>3</id>
                    <description>Compoany 3</description>
               </company>
               <company>
                    <id>7</id>
                    <description>Company 7</description>
               </company>
          </supplier>
          ... <!-- a lot of supplier --> ...
          <supplier>
               <agreement>3</agreement>
               <company>
                    <id>1</id>
                    <description>Company 1</description>
               </company>
               <company>
                    <id>18</id>
                    <description>Company 18</description>
               </company>
          </supplier>
     </gruppi>        
</root>

我的目标是拥有一个能够执行以下处理的模板:对于gruppi中的每个供应商,我希望输出记录集name =“companies”中所有记录的列表,除了ID已包含在id中的记录当前供应商要素的元素后代。

换句话说:对于每个供应商,我应该创建一组html选项,列出所有公司(id为值),不包括已包含在供应商中的公司

我尝试使用xsl:key和一些递归,但我发现这个任务非常复杂,而且我的想法已经用完了。有人提示要解决这个问题吗?

感谢您的帮助!

@polishchuk:xml输入是一个非常大的文件,所以我提取了我感兴趣的结构,填充了一些通用数据。关于输出我正在寻找这样的东西:

<h1>Agreement: 1</h1>
<select>
     <option value="1"></option>
          ....                                                  <!-- WITHOUT values 3 and 7 -->
     <option value="N"></option>
</select>

<h1>Agreement: 3</h1>
<select>
     <option value="1"></option>
          ....                                                  <!-- WITHOUT values 1 and 18 -->
     <option value="N"></option>
</select>

1 个答案:

答案 0 :(得分:0)

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="//supplier"/>
    </xsl:template>

    <xsl:template match="supplier">
        <h1>
            <xsl:text>Agreement:&#xA0;</xsl:text>
            <xsl:value-of select="agreement"/>
        </h1>
        <select>
            <xsl:for-each select="//recordset[@name = 'companies']/record[not(id = current()/company/id)]">
                <option value="{id}">
                    <xsl:value-of select="id"/>
                </option>
            </xsl:for-each>
        </select>
    </xsl:template>
</xsl:stylesheet>

假设XML:

<root>
    <recordset name="companies">
        <record>
            <id>1</id>
            <description>Company1</description>
        </record>
        <record>
            <id>2</id>
            <description>Company2</description>
        </record>
        <record>
            <id>3</id>
            <description>Company3</description>
        </record>
        <record>
            <id>4</id>
            <description>Company4</description>
        </record>
        <record>
            <id>5</id>
            <description>Company5</description>
        </record>
    </recordset>
    <gruppi>
        <supplier>
            <agreement>1</agreement>
            <company>
                <id>1</id>
            </company>
            <company>
                <id>2</id>
            </company>
        </supplier>
        <supplier>
            <agreement>2</agreement>
            <company>
                <id>1</id>
            </company>
            <company>
                <id>3</id>
            </company>
        </supplier>
        <supplier>
            <agreement>3</agreement>
            <company>
                <id>4</id>
            </company>
            <company>
                <id>5</id>
            </company>
        </supplier>
    </gruppi>
</root>

输出:

<h1>Agreement: 1</h1>
<select>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>
<h1>Agreement: 2</h1>
<select>
  <option value="2">2</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>
<h1>Agreement: 3</h1>
<select>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>