Like sibling processing template(s) needed

M

Mike Conmackie

Hello,

I have a situation where I need to group an arbitrary number of like sibling
input elements into a compound output element as follows:

Input:
<feature>
 
C

Chris Huebsch

Mike Conmackie (Thu, 29 Apr 2004 14:06:11 -0400):
Input:
<feature>
.
.
<hostid type="ethernet">0004d355b321</hostid>
<hostid type="ethernet">000123456789</hostid>
<nostid type="ethernet">000987654321</hostid>
.
.
</feature>

Output:
<key>
.
.
<hostid>
<Eth>0004d355b321</Eth>
<Eth>000123456789</Eth>
<Eth>000987654321</Eth>
</hostid>
.
.
</key>

<xsl:template>
<xsl:if test="//hostid[@type='ethernet']">
<hostid>
<xsl:for-each select="//hostid[@type='ethernet']">
<Eth><xsl:value-of select="."/></Eth>
</xsl:for-each>
</hostid>
</xsl:if>
</xsl:template>

I think this solution lacks some elegance, but I think it will work.


Chris
 
M

Mike Conmackie

Chris Huebsch said:
Mike Conmackie (Thu, 29 Apr 2004 14:06:11 -0400):
Input:
<feature>
.
.
<hostid type="ethernet">0004d355b321</hostid>
<hostid type="ethernet">000123456789</hostid>
<nostid type="ethernet">000987654321</hostid>
.
.
</feature>

Output:
<key>
.
.
<hostid>
<Eth>0004d355b321</Eth>
<Eth>000123456789</Eth>
<Eth>000987654321</Eth>
</hostid>
.
.
</key>

<xsl:template>
<xsl:if test="//hostid[@type='ethernet']">
<hostid>
<xsl:for-each select="//hostid[@type='ethernet']">
<Eth><xsl:value-of select="."/></Eth>
</xsl:for-each>
</hostid>
</xsl:if>
</xsl:template>

I think this solution lacks some elegance, but I think it will work.


Chris
--
Chris Huebsch www.hübsch-gemacht.de | TU Chemmnitz, Informatik, RNVS
GPG-Encrypted mail welcome! ID:7F2B4DBA | Str. d. Nationen 62, B204
Chemnitzer Linux-Tage 2005, 5.-6.März | D-09107 Chemnitz
http://www.tu-chemnitz.de/linux/tag/ | +49 371 531-1377, Fax -1803

Chris,

I may have lead you astray with a rather simple example. In actuality, the
input could look like this:

<feature>
 
C

Chris Huebsch

Mike Conmackie (Thu, 29 Apr 2004 14:42:28 -0400):
I may have lead you astray with a rather simple example. In actuality, the
input could look like this:

Yes you did :)
<feature>
.
.
<hostid type="ethernet">0004d355b321</hostid>
<hostid type="internet">127.0.0.1</hostid>
<hostid type="serial">1DFFF375281007509B</hostid>
<hostid type="softkey">D12C7143E283D2E</hostid>
.
.
</feature>
I thought that if I could get an XPath expression that would create a node
set comprised of similar siblings at a given input tree location then I
could iterate over the set. Does this sound reasonable?

It does.

But let me ask - can you send some more lines of your xml-document?
Perhaps an other host?

Does it look like this:

<hostid type="ethernet">000abacde321</hostid>
<hostid type="internet">127.0.0.2</hostid>
<hostid type="serial">1DFFF3ddd3334555gB</hostid>
<hostid type="softkey">111122223333444</hostid>
<hostid type="ethernet">0004d355b321</hostid>
<hostid type="internet">127.0.0.1</hostid>
<hostid type="serial">1DFFF375281007509B</hostid>
<hostid type="softkey">D12C7143E283D2E</hostid>

If the answer is yes - are there allways those 4 hostid-elements?


Chris
 
M

Mike Conmackie

Hi Chris,
But let me ask - can you send some more lines of your xml-document?
Perhaps an other host?

I e-mailed you a sample XML input document.
Does it look like this:



If the answer is yes - are there allways those 4 hostid-elements?

Yes, it could look like that. There can be from 1 to n <hostid> tags and
the value of the "type" attribute can be any one of a fixed set of 4 values
{ethernet, serial, softkey, internet}. Hope this helps.

Mike
 
C

Chris Huebsch

Mike Conmackie (Thu, 29 Apr 2004 19:11:21 -0400):
Yes, it could look like that. There can be from 1 to n <hostid> tags and
the value of the "type" attribute can be any one of a fixed set of 4 values
{ethernet, serial, softkey, internet}. Hope this helps.

Ok. I tried the following templates:

<xsl:template match="feature">
<xsl:element name="key">
<xsl:attribute name="protected" />
<xsl:apply-templates select="name|version|start|expires|count"/>
<xsl:call-template name="hostids"/>
</xsl:element>
</xsl:template>

<xsl:template name="hostids">
<xsl:if test="hostid[@type='ethernet']">
<hostid>
<xsl:apply-templates select="hostid[@type='ethernet']">
<xsl:with-param name="element">Eth</xsl:with-param>
</xsl:apply-templates>
</hostid>
</xsl:if>
<xsl:if test="hostid[@type='serial']">
<hostid>
<xsl:apply-templates select="hostid[@type='serial']">
<xsl:with-param name="element">Serial</xsl:with-param>
</xsl:apply-templates>
</hostid>
</xsl:if>
<xsl:if test="hostid[@type='softkey']">
<hostid>
<xsl:apply-templates select="hostid[@type='softkey']">
<xsl:with-param name="element">Softkey_id</xsl:with-param>
</xsl:apply-templates>
</hostid>
</xsl:if>
</xsl:template>

<xsl:template match="hostid">
<xsl:param name="element"/>
<xsl:element name="{$element}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>


Its not really nice. Esp. the 3 rather similar if-s.


Regards


Chris
PS: I'll mail you the complete template back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top