Forum Moderators: open

Message Too Old, No Replies

xsl value-of help

         

blakogre

12:04 am on Sep 21, 2007 (gmt 0)

10+ Year Member



I have a statement in my XSL sheet:

<xsl:value-of select="general/alert[id='12053']/desc"/>

It returns in the outputted file:

"10.10.10.24 resolves as hostname.domainname.com.<br/>"

I really only want:

"10.10.10.24 resolves as hostname.domainname.com."

Or even "hostname.domainname.com"

Any way to parse/select only that portion? hostname.domainname.com is a variable, obviously, and is different for each IP/host.

cmarshall

1:58 am on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unfortunately, you have to use XSLT/XPath 2 [xml.com] to do a proper RegEx.

If you are using PHP to execute your XSL, then you are stuck XSLT/XPath 1.0, which means that you need to use kind of a pathetic lash-up, like so:

XML:

<desc>10.10.10.24 resolves as hostname.example.com.[red][b]&lt;br/&gt;[/b][/red]</desc>

Note that I escaped the "<br/>". This is because proper XML will interpret this as a tag, and it will not be included in the string, so yours must be declared that way.

This XSLT works:

XSLT:

<xsl:variable name="spaz" select="general/alert[@id='12053']/desc"/>

<xsl:value-of select="substring-before(substring-after($spaz,'as '), '.&lt;br')"/>

blakogre

4:06 am on Sep 21, 2007 (gmt 0)

10+ Year Member



I'm at home now, so will have to try it out tomorrow. Unfortunately, it's a bit of an odd situation than posters might generally come here with.

I'm working with reports generated by Nessus, a vulnerability scanner. It creates the XML, so I have no control over that (to escape, as you suggested).

They do provide XSL sheets, and via their client software, creates HTML output with the XSL/XML. So, all I have control over, is the XSL. I'm not sure if their client uses PHP or...?

I'll tinker with the feedback you gave, and hopefully can get something workable. I may post a follow up question here, but if there's a good template/example site that includes more info/examples on the substring technique, I'm happy to reference that as well if anyone can provide a link. I spent a lot of time googling for value-of tips/tricks but came up empty. Thanks much.

cmarshall

10:38 am on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If they have their XML looking like this:

<desc>10.10.10.24 resolves as hostname.example.com.[red][b]<br/>[/b][/red]</desc>

Then you should not be getting the

<br/>
included in your string.

Like I said, if they accept XSLT 2, then you have a lot more to work with. XPath 2 allows RegEx.

I had to break the XPath into two for my example, because libxslt [xmlsoft.org] (the XSLT 1 processor used by PHP) wouldn't do a proper substring parse of the direct node.

I tried doing this:

<xsl:value-of select="substring-before(substring-after([red][b].[/b][/red],'as '), '.&lt;br')"/>

but it didn't work. I needed to put it in a "variable" first.

Hope it works out for you.

blakogre

2:20 pm on Sep 21, 2007 (gmt 0)

10+ Year Member



Here's the actual XML:

<alert>
<hostname>10.10.10.10</hostname>
<portname>general/tcp</portname>
<id>12053</id>
<level>NOTE</level>
<desc><![CDATA[10.10.10.10 resolves as hostname.domainname.com.<br/>]]></desc>
</alert>

Will be trying it shortly...

cmarshall

2:25 pm on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh, of course, a CDATA section! D'oh!

This should work, but you might need to monkey with it a wee bit.

blakogre

2:50 pm on Sep 21, 2007 (gmt 0)

10+ Year Member



:-)

Thanks -- you solved it!

Here's what worked:
<xsl:variable name="spaz" select="general/alert[id='12053']/desc"/>
<xsl:value-of select="substring-before(substring-after($spaz,'as '), '.&lt;br')"/>

(I removed the @ before "id=" in the first line. Prior to that, no results. After that -- beautiful.

No other changes needed. Output: perfect.

I'm planning on submitting these modified templates (which are improved over the originals) back to TenableSecurity.com / nessus.org, and will include a thanks to you in there for getting me past the last hurdle. I'm hoping they'll distribute them with the windows package.

cmarshall

2:59 pm on Sep 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad it worked out for you, and welcome to WebmasterWorld!