使用Javascript代码将Cognos XML Schema文件转换为XML

时间:2012-02-27 13:19:10

标签: javascript xml xsd xml-parsing cognos

我在网上发现了许多“转换器”,但是我找不到任何可以轻松实现的源代码。我想知道是否有人会如此友善地向我提供建议,甚至为我提供代码来为我完成这项任务。我正在尝试转换为XML,因此解析起来会更容易。

我正在尝试转换此内容(Cognos XML Schema)...

  <?xml version="1.0" encoding="utf-8" ?> 
- <dataset xmlns="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
- <!-- 
<dataset
    xmlns="http://developer.cognos.com/schemas/xmldata/1/"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocation="http://developer.cognos.com/schemas/xmldata/1/ xmldata.xsd"
>


--> 
- <metadata>
  <item name="ROOT_NODE_ID" type="xs:decimal" precision="38" /> 
  <item name="LEVEL1_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL1_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL2_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL2_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL3_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL3_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL4_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL4_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL5_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL5_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL6_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL6_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL7_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL7_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL8_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL8_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL9_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL9_NAME" type="xs:string" length="512" /> 
  <item name="LEVEL10_ID" type="xs:string" length="2002" /> 
  <item name="LEVEL10_NAME" type="xs:string" length="512" /> 
  </metadata>
- <data>
- <row>
  <value>5</value> 
  <value>5</value> 
  <value>Global Root</value> 
  <value>41</value> 
  <value>Company</value> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  </row>
- <row>
  <value>5</value> 
  <value>5</value> 
  <value>Global Root</value> 
  <value>41</value> 
  <value>Company</value> 
  <value>101590</value> 
  <value>Customer</value> 
  <value>101591</value> 
  <value>Customer -All Sites</value> 
  <value>125083</value> 
  <value>Site 1</value> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  <value xs:nil="true" /> 
  </row>
  </data>
  </dataset>

到此(XML)....

<?xml version="1.0" encoding="UTF-8"?>
<rows>
  <row>
    <ROOT_NODE_ID>5</ROOT_NODE_ID>
    <LEVEL1_ID>5</LEVEL1_ID>
    <LEVEL1_NAME>Global Root</LEVEL1_NAME>
    <LEVEL2_ID>41</LEVEL2_ID>
    <LEVEL2_NAME>Company</LEVEL2_NAME>
    <LEVEL3_ID></LEVEL3_ID>
    <LEVEL3_NAME></LEVEL3_NAME>
    <LEVEL4_ID></LEVEL4_ID>
    <LEVEL4_NAME></LEVEL4_NAME>
    <LEVEL5_ID></LEVEL5_ID>
    <LEVEL5_NAME></LEVEL5_NAME>
    <LEVEL6_ID></LEVEL6_ID>
    <LEVEL6_NAME></LEVEL6_NAME>
    <LEVEL7_ID></LEVEL7_ID>
    <LEVEL7_NAME></LEVEL7_NAME>
    <LEVEL8_ID></LEVEL8_ID>
    <LEVEL8_NAME></LEVEL8_NAME>
    <LEVEL9_ID></LEVEL9_ID>
    <LEVEL9_NAME></LEVEL9_NAME>
    <LEVEL10_ID></LEVEL10_ID>
    <LEVEL10_NAME></LEVEL10_NAME>
 </row>
 <row>
    <ROOT_NODE_ID>5</ROOT_NODE_ID>
    <LEVEL1_ID>5</LEVEL1_ID>
    <LEVEL1_NAME>Global Root</LEVEL1_NAME>
    <LEVEL2_ID>41</LEVEL2_ID>
    <LEVEL2_NAME>Company</LEVEL2_NAME>
    <LEVEL3_ID>101590</LEVEL3_ID>
    <LEVEL3_NAME>Customer</LEVEL3_NAME>
    <LEVEL4_ID>101591</LEVEL4_ID>
    <LEVEL4_NAME>Customer -All Sites</LEVEL4_NAME>
    <LEVEL5_ID>125083</LEVEL5_ID>
    <LEVEL5_NAME>Site 1</LEVEL5_NAME>
    <LEVEL6_ID></LEVEL6_ID>
    <LEVEL6_NAME></LEVEL6_NAME>
    <LEVEL7_ID></LEVEL7_ID>
    <LEVEL7_NAME></LEVEL7_NAME>
    <LEVEL8_ID></LEVEL8_ID>
    <LEVEL8_NAME></LEVEL8_NAME>
    <LEVEL9_ID></LEVEL9_ID>
    <LEVEL9_NAME></LEVEL9_NAME>
    <LEVEL10_ID></LEVEL10_ID>
    <LEVEL10_NAME></LEVEL10_NAME>
  </row>
</rows>

1 个答案:

答案 0 :(得分:0)

此Xslt从任意Cognos报告中剥离任何模式元数据 - Xml输出方法 - 并创建从元数据中提取名称的结果元素:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns="http://tempuri.org/" xmlns:cog="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="//comment()" />

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

    <xsl:template match="cog:dataset">
        <rows>
            <xsl:apply-templates />
        </rows>
    </xsl:template>

    <xsl:template match="cog:metadata">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="cog:item">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="@name | @type | @length | @precision" />

    <xsl:template match="cog:data">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="cog:row">
        <row>
            <xsl:apply-templates />
        </row>
    </xsl:template>

    <xsl:template match="cog:value">
        <xsl:variable name="currentposition" select="count(./preceding-sibling::cog:value)+1" />
        <xsl:variable name="currentname" select="//cog:metadata/cog:item[$currentposition]/@name" />
        <xsl:element name="{$currentname}">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>

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

如果你想让Cognos应用服务器端转换,你必须选择 XHTML 输出方法,这将使转换更容易一些每个字段都将带有实际名称,而不是 value