dhinu
dhinu

Reputation: 135

Need to validate a string, whether it contains html or xml using xslt

I have an xsl:variable whose contents might be HTML or XML or binary.

I'm displaying the value of the variable in a textarea in a html page.

If the variable contains HTML or XML data, it is displayed unformatted in the textarea.

<xsl:variable name="outputString">
//html or xml or binary data goes in here
</xsl:variable>
<xsl:template match="/">
<html>
<body>
<textarea name="output" cols="20" rows="20">
<xsl:value-of select="$outputString" />
</textarea>
</body>
</html>
</xsl:template>

All I need is to display the contents of the variable in a formatted way inside the textarea if the contents are either HTML or XML.

Upvotes: 0

Views: 373

Answers (2)

Fawix
Fawix

Reputation: 469

Well I would need to try it out, but I believe you could do the following

<xsl:if test="fn:contains($outputString, '<(.*)>.*<\1>')>
</xsl:if>

or in your case I would rather use the choose tag.

The fn:contains() is for XSLT 2.0 and the part I'm not sure is weather it will accept the regex in that format. even more because some places use $1 instead of \1 for referencing the group value.

If you are referencing a XML or HTML that would detect it though.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163595

You'll need processor extensions to do this job, so the answer depends on which XSLT processor you are using.

Upvotes: 1

Related Questions