views:

20

answers:

2

Hi I am usually programming in c++ so XML/XSL/XPATH is not my strong side, but I need to do a transformation and I cannot seem to find a good way of doing it.

We have an xml file that is formatted like this:

<outer>
  <element/>
  <other_element/>
  <message pri="info">
    [[!CDATA Error: something is not working]] 
  </message>
  <message pri="info">
    [[!CDATA Warning: warnings are boring]] 
  </message>
</outer>

I need an xsl that can transform this to the exact same xml file, with the exception, that it matches the "Error:" part, and changes the pri attribute to "error".

I need to do this with an xsl transformaton, and I have started looking at XPATH, but I have a hard time finding out how I can accomplish this.

I got as far as matching the messages with a

<xsl:template match="message">
  <xsl:choice>
    <xsl:when test="tricky part">
    </xsl:when>
    <xsl:otherwise>
      <xsl:justcopythetag>
    </xsl:otherwise>
  </xsl:choice>
</xsl:template>

I have found out how to copy the other message tags, but I have no clue on how to match the "error" part.

Can anyone help me?

+2  A: 

You're just looking for the string "Error:" right?

<xsl:when test="contains(text(),'Error:')">
</xsl:when>

The contains() returns true if the first parameter contains the second.

Welbog
So text() will refer to the matched node? I see. Thank you
daramarak
`text()` refers specifically to the string contents of a node. It's not necessary in most cases, but I personally find it clearer and easier to understand at a glance to use `text()` over `.`.
Welbog
I agree, text() are clearer.
daramarak
+2  A: 
...
<xsl:when test="contains(., 'Error')">
    <message pri="error"><xsl:value-of select="."/></message>
</xsl:when>
...otherwise
aefxx
This is great. This looks like the complete solution to my problem.To bad I cannot split my answer...
daramarak
Never mind, you're welcome!
aefxx
A question, doesn't this actually place a <message> tag within the already matched <message> tag?
daramarak
Your matched <message> tag isn't automatically transformed into your resulting XML/HTML/whatever document. You have to explicitly state what to put out (in this case either the original message or the altered one).
aefxx
I understand, but when saying <message> <xsl:value-of select="."/></message> doesn't the select="." include the matched <message>?
daramarak
Just the "inner" part between the opening and closing tags.
aefxx