1

I want to do some image detection based on payload information in XSLT (with Xpath 2.0). I have found out that all images of the format I wish to detect start with the characters "jP" or "OQ" in the payload. When the element contains the substrings "JP" or "OQ" as the first characters in the element, I want to create a new element yes or something similar, based on which my information flow (Oracle SOA suite 12c) will go a certain direction. I have an example of my payload and my current XSLT underneath. My question is why my XSLT does not function yet, and if you would tackle my topic otherwise.

My payload looks something like this:

             <ns3:BlablablaExample>
                <ns3:Attachments>
                   <ns3:Images>
                      <ns3:Image>
                         <ns3:AttachmentID>11223344</ns3:AttachmentID>
                         <ns3:MetaData>
                            <ns3:OriginalTimestamp>2020-11-10T10:52:07.539Z</ns3:OriginalTimestamp>
                            <ns3:CreationTimestamp>2020-11-10T10:52:07.539Z</ns3:CreationTimestamp>
                         </ns3:MetaData>
                         <ns3:DataSource>jPRandOmD4t4AndSTu77/9j/4AAQSkZJRgABAQAAAQABAAD/5QAJSWRlbnRpeP/bAEMABgQFBgUEBgYFBgcHBggKEAoKCQkKFA4PDBAXFBgYFxQWFhodJR8aGyMcFhYgLCAjJicpKikZHy0wLSgwJSgpKP/bAEMBBwcHCggKEwoKEygaFhooKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCullullkokKoKokokCgoKCgoKCgoKCgoKP/AABEIAoAB4AMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h4xxF0R3alOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPEigAooooAKKKKACiiigAooooAKKKKACiiigAooo))))0000oo)oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooo))oo)))000oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP//Z</ns3:DataSource>
                      </ns3:Image>
                   </ns3:Images>
                </ns3:Attachments>
             </ns3:BlablablaExample>

I imagine my XSLT will look something like this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:ns3="http://www.randomnamespace.com/example/awesomestuff">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="text()"/>
        <xsl:for-each select="ns3:BlablablaExample/ns3:Attachments/ns3:Images/ns3:Image">
            <xsl:choose>
                    <xsl:when test="substring(/ns3:DataSource,1,2)" == "jP" || "OQ">
                        <JPEG2000>Yes</JPEG2000>
                    </xsl:when>
                    <xsl:otherwise>
                        <JPEG2000>No</JPEG2000>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

However, as I mentioned, the xslt doesnt work. My question is, what is wrong with the above XSLT, and how in your opinion would you perhaps tackle this problem otherwise? :)

Thank you kindly!

Jesper

Jesper Vernooij
  • 173
  • 1
  • 15

1 Answers1

4

"My question is, what is wrong with the above XSLT"

There are so many things wrong with your XSLT sample that it's hard to know where to begin. It's syntactically invalid XML. It's semantically invalid XSLT. It contains invalid XPath. XPath has no || operator, and even if it had, the construct if x is a or b means "if x is a or x is b" in next to no programming language in existence. There are a couple of other things, but I guess that's already taking your question a little too literally.

You meant to write something like this:

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xp20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
  xmlns:ns3="http://www.randomnamespace.com/example/awesomestuff"
>
  <xsl:output method="xml" indent="yes" encoding="UTF-8"/>

  <xsl:template match="ns3:Image">
    <JPEG2000>
      <xsl:choose>
        <xsl:when test="substring(ns3:DataSource,1,2) = ('jP','OQ')">Yes</xsl:when>
        <xsl:otherwise>No</xsl:otherwise>
      </xsl:choose>
    </JPEG2000>
  </xsl:template>
</xsl:stylesheet>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 3
    You owe Tomalak an upvote, an accept, and a beer for this gracious and complete answer. – kjhughes Dec 05 '20 at 19:31
  • 1
    All three are certainly in order! Thank you Tomalak for your quick and complete answer, I learn a lot from replies like this. – Jesper Vernooij Dec 07 '20 at 08:34
  • 1
    @Jesper Glad to hear that you didn't take the flak personally, that's not the norm these days. – Tomalak Dec 07 '20 at 08:42
  • People are getting more and more entitled to things that are not to be taken for granted. I couldn't do what I do without the help of the community, so I'm always glad to be put right any way that works. Sometimes it brings the message across better as well. This makes me look at a lot of code (xslt) I might have adapted, written (not that much yet) and learned from in the past that might need a quick revisit. Nothing but love for the people me look at things differently. – Jesper Vernooij Dec 07 '20 at 09:01
  • 2
    @Jesper On a technical note, it might be smarter to have `JPEG2000` instead of `Yes`. This way it would make it easy to accommodate other types without having to change the XML structure, should you ever expect other file types. – Tomalak Dec 07 '20 at 09:07