Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

Conversion of XML to VXML using XSLT

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 of xsl: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.)

<myProjtag>
  <prompt>I am here</prompt>
  <prompt>I am not here</prompt>
</myProjtag>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!--Identity tempate.-->
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/">
    <vxml version="2.0" lang="en">
      <form id="{substring(local-name(/*), 0, string-length(local-name(/*))-2)}">
        <xsl:apply-templates select="*/*"/>
      </form>
    </vxml>    
  </xsl:template>

</xsl:stylesheet>

XML Output

<vxml version="2.0" lang="en">
   <form id="myProj">
      <prompt>I am here</prompt>
      <prompt>I am not here</prompt>
   </form>
</vxml>

If you have any questions on the XSLT, running Saxon from the command line, etc., just let me know.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...