2

The following is the sample XML structure I'm working on:

<command name="test">
      <parameter index="2">4000</parameter>
      <tag>4000</tag>
      <parameter index="3">tag</parameter>
      <parameter index="4">4000</parameter>
    </command>

<command name="test">
  <parameter index="2">4000</parameter>
  <add>
    <parameter index="1">ports</parameter>
    <parameter index="2">1:1,</parameter>
    <parameter index="3">3:1,</parameter>
    <parameter index="4">3:9-12,</parameter>
    <parameter index="5">4:12</parameter>
  </add>
  <parameter index="3">add</parameter>
  <parameter index="4">ports</parameter>
  <parameter index="5">1:1,</parameter>
  <parameter index="6">3:1,</parameter>
  <parameter index="7">3:9-12,</parameter>
  <parameter index="8">4:12</parameter>
  <tagged />
  <parameter index="9">tagged</parameter>
</command>

And the code snippet on the XSL file is:

 <xsl:key name="key" match="command[@name='test'][count(tag) &gt; 0]" use="parameter[@index='2']"/>
    <xsl:key name="port" match="command[@name='test'][count(add) &gt; 0]" use="add/parameter"/>

<xsl:template match="xyz">
<xsl:variable name="portid" select="concat($slot-no,concat(':',$port-no))"/>
<xsl:apply-templates select="key('port',$portid)"/>
</xsl:template>

<xsl:template match="command[@name='test']">
         <xsl:variable name="name" select="parameter[@index=2]"/>
         <object>
         <name><xsl:value-of select="$name"/></name>
                  <class>XYZ</class>
            <attributes>
               <attribute>
                  <name>XYZ1</name>
                  <value><xsl:value-of select="key('key',$name)/tag"/></value>
               </attribute>
            </attributes>
         </object>
</xsl:template>

The variable 'portid' is in the form 'x:x', where x is a number. For each of the portid, I need to associate with the <parameter index="2"> value. Previously we had only one portid value under the <add> node and the solution was working fine.

Now, I need to change the 'use' expression in the XSL key 'port' so that the values are changed from '1:1,' to '1:1' and similarly '3:1,' to '3:1' and expand '3:9-12,' to '3:9' , '3:10' , '3:11' , '3:12' and store them with the value in <parameter index="2">. For example, each time the 'portid' is any one of this '1:1', '3:1', '3:9' , '3:10' , '3:11', '3:12' and '4:12', the value to associate is '4000'.

Is this possible? I'm working on this for a week and still not able to find a solution. Any help would be really appreciated. Thanks a lot guys.

Sanjay
  • 21
  • 1
  • 4
  • Are you able to use XSLT 2.0? If not, we can suggest a workaround for 1.0, but it's not very pretty. – LarsH Aug 26 '11 at 17:24
  • 1
    Could you, please provide the wanted result and describe the required rules for the grouping? This isn't clear so far. – Dimitre Novatchev Aug 27 '11 at 01:35
  • @LarsH: I could only use XSLT 1.0. Thanks for your help. – Sanjay Aug 30 '11 at 16:05
  • @Dimitre Novatchev: I'm sorry if my question wasn't clear enough. I've edited the original question to give more detail into the problem. Thank you for your time. – Sanjay Aug 30 '11 at 16:15

2 Answers2

1

I think you can only do that cleanly with XSLT 2.0 e.g.

<xsl:key name="port" match="command[@name='test'][add]" use="add/parameter/replace(., ',', '')"/>

will do for the simply replacement, for the more complex one you will probably have to write a function with xsl:function that takes e.g. '3:9-12,' and returns the sequence you want, that shouldn't be to difficult with XPath 2.0's string functions.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thank you very much for taking time to help me with the answer. Unfortunately we could only use the XSLT 1.0. I've also added more clarity to the question (I Hope!). I'll be waiting for your advice on this. Thanks again. – Sanjay Aug 30 '11 at 16:55
0

I was able to find the solution for this problem by not using the XSL key. Instead I used a call-template method to strip the commas and expand the series and find the match. Thanks to all who cared to help me on this

Sanjay
  • 21
  • 1
  • 4