Conversion of XML to VXML using XSLT

2019-09-21 09:03发布

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>

标签: xml xslt
2条回答
男人必须洒脱
2楼-- · 2019-09-21 09:41

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:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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


<xsl:template match="/*">
<form>
   <xsl:attribute name="id">
      <xsl:value-of select="substring(local-name(), 0, string-length(local-name())-2)"/>
   </xsl:attribute>
   <!-- * is prompt elements -->
   <xsl:apply-templates select="*"/>
</form>
</xsl:template>


<xsl:template match="/">
    <vxml version="2.0" lang="en">
            <!-- * is myProjTag element -->
            <xsl:apply-templates select="*"/>
    </vxml>
</xsl:template> 
</xsl:stylesheet>
查看更多
Melony?
3楼-- · 2019-09-21 10:00

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.

查看更多
登录 后发表回答