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

时间:2023-01-29
本文介绍了使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:带有 XML 字符串内容的 Edge 中的 Microsoft.XMLDOM XML 解析器引发错误 下一篇:尝试使用 DOMParser 解析 html 字符串时遇到的麻烦

相关文章

最新文章