Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...