Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: Reading a param that's an xml document passed as a string

Tags:

string

xml

xslt

I have ran into an issue with another engineer. Being a new engineer, I have to figure out the solution to this, but I haven't been able to. Any help would be appreciated, the closest thing I've found is Chunk string with XSLT but is still not quite there. I'm only able to use XSL v1.0.

A parameter is passed to my stylesheet as a giant string. It originally came from an XML Document. The string looks something like this.

With <xsl:value-of select="$servers"/> where $servers is the param for the string passed to me. The string looks like this:

<license><active_servers><server><name>MIKE</name><capacity>18</capacity><status>0</status><expiration></expiration><left>0</left><comment></comment></server><server><name>Susie</name><capacity>0</capacity><status>1</status><expiration>2014-07-04T00:00:00Z</expiration><left>5238568</left><comment></comment></server><server><name>Zoe</name><capacity>5000</capacity><status>1</status><expiration></expiration><left>0</left><comment></comment></server></active_servers></license>

This is a section of the xml data passed as a param to the stylesheet. The actual document is 300 or so lines of data. The only distinctive thing that separates these "nodes" are the <server> and </server>. Is there a way to pull data out of this if it's a really big string?

Say, for example. I need to find "Zoe" and see if she has a "expiration", if she doesn't, I need her "status". So Zoe will display "1" for status since she doesn't have expiration. While MIKE will display "0" and Susie will display 2014-07-04T00:00:00Z.

I have looked all over google and stack overflow for finding a solution to parsing/reading a giant string. But I haven't come to a solution close enough where I can make it work. As of right now, I'm stuck having no working copy, and 2 days into research without getting anywhere.

like image 624
misterbear Avatar asked Dec 13 '25 17:12

misterbear


1 Answers

A string is not XML and cannot be parsed as XML. You could of course parse the string as string, using the string functions provided by XSLT 1.0 - which would be quite tedious and error-prone.

If possible, pass a path to an actual XML document as the parameter. Alternatively, call two transformations in series, with the first transformation saving the parameter to a file with a known path, and the second transformation reading from that file.

See also:
https://stackoverflow.com/a/14512924/3016153

like image 92
michael.hor257k Avatar answered Dec 15 '25 13:12

michael.hor257k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!