XSLT: need help with xsl:for-each

G

Gerald Aichholzer

Hello NG,

I try to apply the following template

<xsl:template match="objectgroup[@objid != '']">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
<xsl:variable name="object" select="."/>
<xsl:for-each select="//plugins/plugin">
<plugin pid="{./@pid}"
href="{//pluginobjectlist[@pid = ./@pid]/
objectgroup[@objid = $object/objid]/@href}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>

to the follwoing xml-file

<symbol>
<objectgroup objid='id1'>
<!-- some elements here -->
</objectgroup>
<plugins>
<plugin pid="plug1"/>
<plugin pid="plug2"/>
<plugin pid="plug3"/>
</plugins>
<plugins>
<pluginobjectlist pid="plug1">
<objectgroup objid="obj1" href="url-p1-o1"/>
<!-- repeats for other objects -->
</pluginobjectlist>
<pluginobjectlist pid="plug2">
<objectgroup objid="obj1" href="url-p2-o1"/>
<!-- repeats for other objects -->
</pluginobjectlist>
<!-- repeats for remaining plugins -->
</plugins>
</symbol>

and I get the following result

<symbol>
<objectgroup objid='id1'>
<!-- some elements here -->
<plugin pid="plug1" href="url-p2-o1"/>
<plugin pid="plug2" href="url-p2-o1"/>
<plugin pid="plug3" href="url-p2-o1"/>
</objectgroup>
<!-- remaining templates remove plugin-elements -->
</symbol>

I see always the same url, so something in the xsl:for-each must
be wrong - but I can't see the problem.

Please give me a hint if you have an idea.

Thanx in advance,
Gerald
 
D

David Carlisle

href="{//pluginobjectlist[@pid = ./@pid]/
objectgroup[@objid = $object/objid]/@href}"/>


@pid (always) means the same as ./@pid so the first filter is always
true if there is a pid attribute. as it is[@pid =@pid] which is [@pid]

In your posted code objectgroup had an objid attribite not an objid
element child so $object/objid is the empty set.



David
 
G

Gerald Aichholzer

David said:
href="{//pluginobjectlist[@pid = ./@pid]/
objectgroup[@objid = $object/objid]/@href}"/>


@pid (always) means the same as ./@pid so the first filter is always
true if there is a pid attribute. as it is[@pid =@pid] which is [@pid]

I thought that the left part of the '='-part denotes the attribute
name to compare. How can I select the pluginobjectlist which pid-
attribute has the same value as the currend pid-attribut in the
xsl:for-each loop?
In your posted code objectgroup had an objid attribite not an objid
element child so $object/objid is the empty set.

Thank you for pointing this out, but fortunately it is a typo only.
The final is (except the open point from above):

href="{//pluginobjectlist[???]/
objectgroup[@objid = $object/@objid]/@href">/


Gerald
 
D

David Carlisle

Gerald Aichholzer said:
David said:
href="{//pluginobjectlist[@pid = ./@pid]/
objectgroup[@objid = $object/objid]/@href}"/>


@pid (always) means the same as ./@pid so the first filter is always
true if there is a pid attribute. as it is[@pid =@pid] which is [@pid]

I thought that the left part of the '='-part denotes the attribute
name to compare.

Both sides of the = are evaluated the same way, as arbitrary XPath
expressions, then their values are compared for equality.
If you start a node-set valued XPath with something other than / or
.. then it's a relative XPath and taken relative to the current node
so @pid is _defined_ as meaning the same thing as ./@pid.

How can I select the pluginobjectlist which pid-
attribute has the same value as the currend pid-attribut in the
xsl:for-each loop?
use a variable to hold the current node or equivalently the
current()/functuion
so something like
@pid = current()/@pid
or
@pid = $object/@pid
(I forget the exact structure of your input)
In your posted code objectgroup had an objid attribite not an objid
element child so $object/objid is the empty set.

Thank you for pointing this out, but fortunately it is a typo only.
The final is (except the open point from above):

href="{//pluginobjectlist[???]/
objectgroup[@objid = $object/@objid]/@href">/


Gerald
 
G

Gerald Aichholzer

Hello David,

David said:
David said:
href="{//pluginobjectlist[@pid = ./@pid]/
objectgroup[@objid = $object/objid]/@href}"/>


[about @pid is the same as ./@pid]

How can I select the pluginobjectlist which pid-
attribute has the same value as the currend pid-attribut in the
xsl:for-each loop?

use a variable to hold the current node or equivalently the
current()/functuion

so something like
@pid = current()/@pid
or
@pid = $object/@pid
(I forget the exact structure of your input)

thank you, but I think I have given an incorrect example
by mistake (which doesn't represent the real problem).
My xml-file looks like this:

<symbol>
<objectgroup objid="abc">
<plugin pid="plugin1" href="..."/>
<plugin pid="plugin3" href="..."/>
</objectgroup>
<objectgroup objid="def">
<!-- ... -->
</objectgroup>
<!-- even more objectgroup elements -->
<plugins>
<plugin pid="plugin1"><desc>...</desc></plugin>
<plugin pid="plugin2"><desc>...</desc></plugin>
<plugin pid="plugin3"><desc>...</desc></plugin>
</plugins>
</symbol>

For each objectgroup/plugin element I'd like to access the
corresponding plugins/plugin element:

<xsl:template match="objectgroup">
<!-- do something -->
<xsl:for-each select="./plugin">
<xsl:value-of select="/symbol/plugins/plugin[???]/desc"/>
^^^
</xsl:for-each> |
<!-- do something --> |
</xsl:template> |
|
+---------------------------------------------------+
v

How can I select /symbol/plugins/plugin haven the same pid
as the current pid in the xsl:for-each loop?

thanks in advance,
Gerald
 
G

Gerald Aichholzer

Hello NG,

I have a XML file looking like this:

<symbol>
<objectgroup objid="abc">
<plugin pid="plugin1" href="..."/>
<plugin pid="plugin3" href="..."/>
</objectgroup>
<objectgroup objid="def">
<!-- ... -->
</objectgroup>
<!-- even more objectgroup elements -->
<plugins>
<plugin pid="plugin1"><desc>...</desc></plugin>
<plugin pid="plugin2"><desc>...</desc></plugin>
<plugin pid="plugin3"><desc>...</desc></plugin>
</plugins>
</symbol>

For each objectgroup/plugin element I'd like to access the
corresponding plugins/plugin element using the following
template:

<xsl:template match="objectgroup">
<!-- do something -->
<xsl:for-each select="./plugin">
<xsl:value-of select="/symbol/plugins/plugin[???]/desc"/>
^^^
</xsl:for-each> |
<!-- do something --> |
</xsl:template> |
|
+---------------------------------------------------+
v

How can I select /symbol/plugins/plugin haven the same pid
as the current pid in the xsl:for-each loop?

thanks in advance,
Gerald
 
D

David Carlisle

didn't we just do this?

<xsl:template match="objectgroup">
<!-- do something -->
<xsl:for-each select="plugin">
<xsl:value-of select="/symbol/plugins/plugin[@pid=current()/@pid]/desc"/>

</xsl:for-each>
<!-- do something -->
</xsl:template>


David
 
G

Gerald Aichholzer

David said:
didn't we just do this?

<xsl:template match="objectgroup">
<!-- do something -->
<xsl:for-each select="plugin">
<xsl:value-of select="/symbol/plugins/plugin[@pid=current()/@pid]/desc"/>

</xsl:for-each>
<!-- do something -->
</xsl:template>

Hello David,

I made it work just a few minutes ago - I think I have misinterpreted
your first answer or I haven't seen the wood from the trees.

thanx,
Gerald
 
G

Gerald Aichholzer

Hello David,

David said:
<xsl:template match="objectgroup">
<!-- do something -->
<xsl:for-each select="plugin">
<xsl:value-of select="/symbol/plugins/plugin[@pid=current()/@pid]/desc"/> ^^^^^^^^^^^^^^^^^

</xsl:for-each>
<!-- do something -->
</xsl:template>

I think I have some problems unterstanding XPath :/

In the above context (see ^^^) there are possibly three
different pid-attributes:

1) pid attribute of the objectgroup (xsl:template match)
which I have to access defining a xsl:variable outside
of the loop (is there also a way without a variable?)

2) pid attribute of xsl:for-each's current element
which I have to access with @pid (or ./@pid)

3) pid attribute of xsl:value-of's element which I have
to access using current()/@pid


Is my above understanding correct?

thanx for your help,
Gerald
 

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,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top