Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top