Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XSLT Get last node name

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  <!-- 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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading