Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is hairdressing stuff! :)

What will it take to get those two node values out from the following XML doc?

<?xml version="1.0" encoding="utf-8"?>
<AlarmSummaryMessage 
  xmlns:cbrn="http://www.site.com/cbrn" 
  xmlns:nc="http://www.site.com/nc" 
  xmlns:scr="http://www.site.com/src"
  xmlns="http://www.site.com/xmlns" xmlns:em="http://www.site.com/em">
  <MessageContent detectionEventKindCode="encounter">
    <AlarmingDevice>
      <EncounterDeviceID>Raid-WTC</EncounterDeviceID>
      <cbrn:EncounterDeviceID2>Raid-WTC</cbrn:EncounterDeviceID2>
    </AlarmingDevice>
  </MessageContent>
</AlarmSummaryMessage>

Using following XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text" indent="no"/>

  <xsl:template match="MessageContent">
    <xsl:text>DEVICE: </xsl:text>
    <xsl:value-of select="AlarmingDevice/EncounterDeviceID/text()"/>
  </xsl:template>

  <xsl:template match="AlarmSummaryMessage">
    <xsl:apply-templates select="MessageContent" />         
  </xsl:template>
</xsl:stylesheet>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Because your XML elments belong to a namespace, you will have to mention that namespace in the XSLT - something like the following will get you started:

<?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:tns="http://www.site.com/xmlns"
    xmlns:cbrn="http://www.site.com/cbrn">

        <xsl:output method="text" indent="no"/>

    <xsl:template match="tns:MessageContent" priority="2">
        <xsl:text>DEVICE:
        </xsl:text>
        <xsl:value-of select="tns:AlarmingDevice/tns:EncounterDeviceID"/>
        <xsl:text>; </xsl:text>
        <xsl:value-of select="tns:AlarmingDevice/cbrn:EncounterDeviceID2"/>
    </xsl:template>

    <xsl:template match="/tns:AlarmSummaryMessage" priority="1">
        <xsl:apply-templates select="tns:MessageContent" />         
    </xsl:template>
</xsl:stylesheet>

Please note the namespace definitions within the xsl:stylesheet tag and the prefixes in front of the node names.

share|improve this answer
    
Dresden and Marty's answer both work fine in VS. –  gerd Jun 19 '13 at 20:00

Unless I misunderstand your question, I believe that you are simply looking for the following:

<xsl:value-of select="/MessageContent/AlarmingDevice/EncounterDeviceID"/>
<xsl:value-of select="/MessageContent/AlarmingDevice/cbrn:EncounterDeviceID2"/>
share|improve this answer
    
I've updated the question with XSLT, which I've been using.. Similar to what you suggest, but it ain't working.. If you strip off all the namespaces from XML it works! –  Ostati Jun 19 '13 at 19:08
    
Above solution from Dresden is not working. –  Ostati Jun 19 '13 at 19:25
    
I agree with Dresden: add a <xsl:value-of select="AlarmingDevice/cbrn:EncounterDeviceID2"/> to your xsl; don't forget to include the namespace in your stylesheet: <xsl:stylesheet ... xmlns:cbrn="site.com/cbrn">; –  gerd Jun 19 '13 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.