开发者

Getting the output of an xslt transformed file in XML format

I have transformed one XML file to an another XML file (in desired structure) using XSLT. However i am unable to view the transformed file in XML format. It shows me plain text values.

This is my original XML file :

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<NewDataSet>
<Table>
 <IMPORT_ID>2</IMPORT_ID>
 <SEQ_NO>1</SEQ_NO>
 <LEVEL_TAG>RANDOMISATIONDATA</LEVEL_TAG>
 <INSERTED>2004-01-21T12:42:53+05:30</INSERTED>
 <INSERTED_BY>kfsv433</INSERTED_BY>
</Table>
   </NewDataSet>

And i coverted it to my desired output using the following XSLT :

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"开发者_StackOverflow
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" indent="yes"/> 
<xsl:strip-space elements="*"/> 
  <xsl:template match="/"> 
  <xsl:apply-templates /> 
 </xsl:template> 
 <xsl:template match="IMPORT_ID"> 
 <IMPORT_ID SEQ_NO="{SEQ_NO/text()}"/> 
 </xsl:template> 
</xsl:stylesheet>

I get the output as

1RANDOMISATIONDATA2004-01-21T12:42:53+05:30kfsv433

I can see that what ever XSLT i have implemented is showing the correct result but i need to get this in XML format.

Kindly help me.


I think you want this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match="IMPORT_ID">
        <IMPORT_ID SEQ_NO="{following-sibling::SEQ_NO[1]/text()}"/>
    </xsl:template>

    <xsl:template match="text()"/>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<NewDataSet>
    <Table>
        <IMPORT_ID>2</IMPORT_ID>
        <SEQ_NO>1</SEQ_NO>
        <LEVEL_TAG>RANDOMISATIONDATA</LEVEL_TAG>
        <INSERTED>2004-01-21T12:42:53+05:30</INSERTED>
        <INSERTED_BY>kfsv433</INSERTED_BY>
    </Table>
</NewDataSet>

the wanted result is produced:

<IMPORT_ID SEQ_NO="1"/>

Explanation:

  1. All text nodes are matched by a template with empty body, which overrides the default XSLT processing, so the text nodes are now not copied to the output.

  2. SEQ_NO is not a child of IMPORT_ID -- it is a sibling.


Do not use a browser. use a real xslt processor such as http://saxon.sourceforge.net.


You are getting the default output, which means that your IMPORT_ID template is not executing.

Try the following templates instead:

<xsl:template match="/"> 
    <xsl:apply-templates select="//Table"/>
</xsl:template> 

<xsl:template match="Table"> 
    <IMPORT_ID SEQ_NO="{SEQ_NO}/{IMPORT_ID}"/> 
</xsl:template>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜