0
votes

Can anyone help me with converting the following XML using XSLT.

My XML file is something like:

<action>
    <item 
        Category ="shop"
        Id="1234"
        Test="1">
        <message>
            Some message
        </message>
    </item>
    <item 
        Category ="rent"
        Id="3456"
        Actual="1"
        Subdivision="333">
        <message>
            Some shops for rent
        </message>
    </item>
    <item>
    </item>
</action>

Now I need to write code in XSLT. If <message> is/contains "shops for rent" then I like to get the result/message "some message".

Can you please help me to solve this.

1

1 Answers

0
votes

The following XSLT outputs the text of the previous <message> node if some other <message> node contains the text "shops for rent".

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

  <xsl:template match="text()" />       <!-- ignores other text content -->

  <xsl:template match="action/item/message[contains(text(),'shops for rent')]">
    <xsl:value-of select="../preceding-sibling::item[1]/message" />
  </xsl:template>
</xsl:stylesheet> 

Output is

Some message

If you would really like to output an xsl:message instead, surround the <xsl:value> with <xsl:message>.