开发者

XSLT task: replicate a list of element for each element of a different list with exceptions

开发者 https://www.devze.com 2023-03-21 13:16 出处:网络
I have some trouble trying to setup a template to solve address this situation: My xml looks like: <root>

I have some trouble trying to setup a template to solve address this situation:

My xml looks like:

<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&开发者_如何转开发gt;
          </supplier>
     </gruppi>        
</root>

My aim is to have a template able to perform the following processing: for each supplier within gruppi I wish to output a list of all record within recordset name="companies" EXCEPT for the record whose id is already contained in the id element descendent of the current supplier element.

In other words: for each supplier I should create a set of html options listing all the companies (id as values) not including the companies already included in the supplier

I am tried to use xsl:key and some recursion but I found this task quite involved and I am running out of ideas. Someone has a hint to solve this problem?

Thanks for any help!

@polishchuk: the xml input is quite a big file, so I extracted the structure I am interested in filling with few generic data. About the output I am looking for something like this:

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


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>

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

Output:

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

精彩评论

暂无评论...
验证码 换一张
取 消