correct xpath expression?

M

mikael petterson

Hi,

I have the following xml-file and need extract the following:

.............

- <enum name="TxDeviceGroup_BbBusState">
<description></description>
- <enumMember name="MASTER_LOADSHARING">
<value>0</value>
</enumMember>
- <enumMember name="MASTER_NO_LOADSHARING">
<value>1</value>
</enumMember>
- <enumMember name="SLAVE">
<value>2</value>
</enumMember>
- <enumMember name="BUS_DISABLED">
<value>3</value>
</enumMember>
</enum>

.....................................

Here is the xsl for retrieving minEnum:
=============================
<xsl:template name="enumMin">

<xsl:param name="str"/>

<!-- Find the enum with the same name as $str ( In this case:
TxDeviceGroup_BbBusState). Find the enumMember

with child <value> that is equal to 0. Then get name attr of element
enumMember.

-->

<xsl:if test="/models/mim/$str/enumMember/value='0'">

<!-- Now get name attr of element enumMember -->

<xsl:value-of select="../@name"/>

</xsl:if>

</xsl:template>

Question 1:

Is this the fastest way to get the attribute value?

Question 2:

When I have a made the following test;

<xsl:if test="/models/mim/$str/enumMember/value='0'">

Will the current node be (using xpath syntax ". ") <value>-tag ? So to
access the parent <enumMember> and it's attribute it is correct to use
.../@name??



//Regards,

//Mikael
 
P

Patrick TJ McPhee

[...]

% - <enum name="TxDeviceGroup_BbBusState">
% <description></description>
% - <enumMember name="MASTER_LOADSHARING">
% <value>0</value>
% </enumMember>

[...]

% <!-- Find the enum with the same name as $str ( In this case:
% TxDeviceGroup_BbBusState). Find the enumMember
%
% with child <value> that is equal to 0. Then get name attr of element
% enumMember.
%
% -->
%
% <xsl:if test="/models/mim/$str/enumMember/value='0'">

This isn't going to work at all. You can't interpolate variables
in location paths, but even if you could, the path wouldn't
be correct. That is, the element of interest to you doesn't
match /models/mim/TxDeviceGroup_BbBusState/enumMember/value.

You're probably trying for this:

/models/mim/enum[@name=$str]/enumMember/value='0'



% <!-- Now get name attr of element enumMember -->
%
% <xsl:value-of select="../@name"/>

Once you get the first problem sorted out, this won't work. The
test in an if element doesn't change the context node. You could
wrap this in for-each

<xsl:for-each select="/models/mim/enum[@name=$str]/enumMember[value='0']">
<xsl:value-of select="@name"/>
</xsl:for-each>

But if you know there will be only one member with this value, you might
as well put the test into the value-of select expression:

<xsl:value-of select=
"/models/mim/enum[@name=$str]/enumMember[value='0']/@name"/>
 
D

David Andriana

mikael petterson said:
- <enum name="TxDeviceGroup_BbBusState">
<description></description>
- <enumMember name="MASTER_LOADSHARING">
<value>0</value>
</enumMember> (snip)
Here is the xsl for retrieving minEnum:
(snip)

Hello,

Try this:

<xsl:template name="enumMin">
<xsl:param name="str"/>

<xsl:variable name="enumMinMember"
select="/models/mim/enum[@name=$str]/enumMember[value=0]"/>

<xsl:if test="$enumMinMember">
<xsl:value-of select="$enumMinMember/@name"/>
</xsl:if>

</xsl:template>
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top