Home > Net >  XSLT Get last node name
XSLT Get last node name

Time:02-17

Hi have this XML for example :

<Message>
        <Ship>
            <ShipSummary>
                <ComName>XPTO 123</ComName>
                <FacName>6</FacName>
            </ShipSummary>
        </Ship>
    </Message>
</tXML>

and I need extract de header 'ComName' for example.

And i try with this code:

  <!-- https://stackoverflow.com/a/10112579/246801 -->
<xsl:template match="text()" mode="header">

    
    <xsl:for-each select="ancestor::*">
      <xsl:choose>
        <!-- avoid beginning slash (at root) -->
        <xsl:when test="position() = 1">
          <xsl:value-of select="local-name()" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat('/',local-name())" />
        </xsl:otherwise>
      </xsl:choose>
      
    </xsl:for-each>
    <xsl:value-of select="$delim"/>
    <!-- <xsl:apply-templates select="node()" /> -->
  </xsl:template>

result is : | Message/Ship/ShipSummary/ComName | Message/Ship/ShipSummary/FacName | | -------- | -------------- | | XPTO 123 | 6 |

And i need this; |ComName | FacName | | -------- | -------------- | | XPTO 123 | 6 |

Any idea?

CodePudding user response:

AFAICT, you only need to do:

<xsl:template match="text()" mode="header">
    <xsl:value-of select="local-name(..)" />
    <xsl:value-of select="$delim"/>
</xsl:template>

Untested, because no code for testing was provided.

  • Related