Schema文件转换为XML

Schema文件转换为XML

本文介绍了使用Javascript代码将Cognos XML Schema文件转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线上找到了许多可以帮助您完成此操作的转换器,但是我找不到能够轻松实现的任何源代码。我想知道是否有人会为我提供建议,甚至为我提供可以帮助我完成此任务的代码。我正在尝试转换为XML,因此将更易于解析。

I found many "converters" online that will do this for you, but I haven't been able to find any source code that I can easily implement. I was wondering if anyone would be so kind to provide me with suggestions or even provide me with code that will do this task for me. I am trying to convert to XML so it will be easier to parse.

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

I am trying to convert this (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)....

To this (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>


推荐答案

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

This Xslt strip any schema metadata from arbitrary Cognos reports - Xml output method - and create result elements with names extracted from the metadata:

<?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

If however you want to make Cognos apply a server-side transformation, you'll have to choose the XHTML output method which will make the transformation a little easier since each field will carry its actual name instead of value.

这篇关于使用Javascript代码将Cognos XML Schema文件转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 08:02