correct xpath expression?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mikael petterson

    #1

    correct xpath expression?

    Hi,

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

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

    - <enum name="TxDeviceG roup_BbBusState ">
    <description> </description>
    - <enumMember name="MASTER_LO ADSHARING">
    <value>0</value>
    </enumMember>
    - <enumMember name="MASTER_NO _LOADSHARING">
    <value>1</value>
    </enumMember>
    - <enumMember name="SLAVE">
    <value>2</value>
    </enumMember>
    - <enumMember name="BUS_DISAB LED">
    <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_B bBusState). 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



  • Patrick TJ McPhee

    #2
    Re: correct xpath expression?

    In article <bk4gm5$5po$1@n ewstree.wise.ed t.ericsson.se>,
    mikael petterson <mikaelpetterso n@noreply.com> wrote:

    [...]

    % - <enum name="TxDeviceG roup_BbBusState ">
    % <description> </description>
    % - <enumMember name="MASTER_LO ADSHARING">
    % <value>0</value>
    % </enumMember>

    [...]

    % <!-- Find the enum with the same name as $str ( In this case:
    % TxDeviceGroup_B bBusState). 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_B bBusState/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"/>

    --

    Patrick TJ McPhee
    East York Canada
    ptjm@interlog.c om

    Comment

    • David Andriana

      #3
      Re: correct xpath expression?

      mikael petterson <mikaelpetterso n@noreply.com> wrote:
      [color=blue]
      > - <enum name="TxDeviceG roup_BbBusState ">
      > <description> </description>
      > - <enumMember name="MASTER_LO ADSHARING">
      > <value>0</value>
      > </enumMember>[/color]
      (snip)[color=blue]
      > Here is the xsl for retrieving minEnum:[/color]
      (snip)

      Hello,

      Try this:

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

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

      <xsl:if test="$enumMinM ember">
      <xsl:value-of select="$enumMi nMember/@name"/>
      </xsl:if>

      </xsl:template>


      --
      David

      Comment

      Working...