I am new to XML and its related languages. I am trying to do a project related to voiceXML. Where I need to convert XML document to VoiceXML document using XSLT. I tried to convert following XML file using xslt. But I am getting an output as: "I am here I am not here I am here I am not here " Can you please help me sort this out?
Thank you in advance.
XML file= "myProj.xml"
<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="myProj_xsl.xsl"?>
<myProjtag>
<prompt>
I am here
</prompt>
<prompt>
I am not here
</prompt>
</myProjtag>
XSLT file="myProj_xsl.xsl"
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<vxml version="2.0" lang="en">
<form id="myProj">
<prompt>
<xsl:value-of select="."/>
</prompt>
<prompt>
<xsl:value-of select="."/>
</prompt>
</form>
</vxml>
</xsl:template>
</xsl:stylesheet>
You need to pay attention to your context node.
<xsl:template match="/">
means your context node is the document. The value of the entire node is just a concatenation of all the text in the document. Thus repeating<xsl:value-of select="."/>
Will give you all the text in the document twice.Try this instead:
Are you trying to process the transformation by opening the XML in a web browser?
If you are, what you're seeing is the browsers attempt to render the output after the transform is complete. Since the browser has no idea how to display vxml, you only see text nodes.
What would help is for you to use an XSLT processor. I'd recommend Saxon. Saxon-HE would be perfect to get you started. The documentation should easily get you running transforms from the command line.
I added another XSLT 1.0 example you can use. The most important piece is the identity template. This will copy all nodes (text/elements/comments/processing instructions) and attributes as-is without modification (as long as they are not overridden by another template). Just add new templates if you need to override the identity template.
Also, I stole Franci Avila's
id
creation but used an AVT instead ofxsl:attribute
. I did this just to show an AVT. AVTs are also very handy to learn.XML Input (I removed the
xml-stylesheet
PI from the input. If I didn't remove it, I'd have to override the identity template to strip it.)XSLT 1.0
XML Output
If you have any questions on the XSLT, running Saxon from the command line, etc., just let me know.