通过XSLT将Congnos 10 XML数据转换为更容易遍历XML文件的方式 [英] Converting Congnos 10 XML Data via XSLT to an easier to loop through XML File

查看:60
本文介绍了通过XSLT将Congnos 10 XML数据转换为更容易遍历XML文件的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到了一个几乎功能齐全的答案:使用Javascript代码将Cognos XML Schema文件转换为XML

I found an almost fully functioning answer here titled: Converting a Cognos XML Schema file to XML using Javascript code

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

我对Filburt出色的解决方案唯一的问题是1.根节点似乎没有创建.(使用Altova的XMLSpy)

The only problem I am having with Filburt's awesome solution is that 1. the root node doesn't seem to be creating. (using Altova's XMLSpy)

当我试图学习如何创建xslt转换时,我正在使用Altova的xmlspy.

I was using Altova's xmlspy when I was trying to learn how to create xslt conversions.

我还注意到Congnos 10正在用空格(即xslt似乎不喜欢抛出QName错误)创建XML元数据标签,即名字.尝试使用Filburt的天才代码时.

I also noticed that Congnos 10 is creating the XML metadata tags with Spaces (which xslt doesn't seem to like Throwing a QName error) on i.e. First Name. When trying to use Filburt's genius bit of code.

一旦我删除了元数据节点中项目名称中的所有空格,我就能使文件正确转换,减去最上面的行标记.

Once I removed all of the spaces in the items names in the metadata nodes I was able to get the file transforming correctly minus the top rows tag.

有人知道为什么那行不通吗?

Any Idea why that wouldn't be working?

我也在尝试为此构建自己的xlst文件,而我唯一会得到的输出是

Also I was trying to build my own xlst file for this and the only output I would get was

示例XML文件

  <?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Congnos2BetterXMLversionSkills.xslt"?>
<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="EmployeeName" type="xs:string" length="2414"/>
    <item name="LastName" type="xs:string" length="202"/>
    <item name="FirstName" type="xs:string" length="202"/>
    <item name="FormerLastName" type="xs:string" length="202"/>
    <item name="EmailAddress" type="xs:string" length="102"/>
    <item name="Skill" type="xs:string" length="2002"/>
</metadata>
<data>
    <row>
        <value>Public, John</value>
        <value>Public</value>
        <value>John</value>
        <value xs:nil="true"/>
        <value>jpublic@email.com</value>
        <value>Language - French</value>
    </row>

</data>
</dataset>

Xslt文件.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <data>
            <xsl:for-each select="/dataset/data/row">
                <row>
                    <Employee_Name>
                        <xsl:value-of select="value[1]"/>
                    </Employee_Name>
                    <Last_Name>
                        <xsl:value-of select="value[2]"/>
                    </Last_Name>
                    <First_Name></First_Name>
                    <Former_Last_Name></Former_Last_Name>
                    <Email_Address></Email_Address>
                    <Skill></Skill>
                </row>
            </xsl:for-each> 
        </data>
    </xsl:template>
</xsl:stylesheet>

也许是标签中的下划线?我只是实施了其中的一部分,以确保我走在正确的轨道上.

Is it perhaps the underscores in the tags? I only implemented part of it as to make sure I was on the right track.

我得到这个作为输出:

<?xml version="1.0" encoding="UTF-8"?>
<data/>

推荐答案

问题出在命名空间上.Cognos XML的开始是这样的:

The issue is with namespaces. The Cognos XML starts like this:

<dataset xmlns="http://developer.cognos.com/schemas/xmldata/1/" ...

这意味着 dataset 元素,并且所有后代节点都是名称空间的一部分.但是,您的XSLT中没有对该名称空间的引用,因此它正在寻找没有名称空间的元素.没有名称空间的 dataset 元素与属于名称空间的元素不同.

This means the dataset element, and all descendant nodes are part of a namespace. However, there is no reference to this namespace in your XSLT, and so it is looking for elements that are in no namespace. A dataset element in no namespace is a different element to one that is part of a namespace.

如果您查看已链接的答案,您将看到样式表上如何定义名称空间前缀 cog ,以及xpath表达式中的所有节点如何使用此前缀.您需要这样做.

If you look at the answer you have linked to, you will see how the namespace prefix cog is defined on the stylesheet, and how all the nodes in xpath expressions use this prefix. You need to do the same.

尝试一下:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cog="http://developer.cognos.com/schemas/xmldata/1/">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <data>
            <xsl:for-each select="/cog:dataset/cog:data/cog:row">
                <row>
                    <Employee_Name>
                        <xsl:value-of select="cog:value[1]"/>
                    </Employee_Name>
                    <Last_Name>
                        <xsl:value-of select="cog:value[2]"/>
                    </Last_Name>
                    <First_Name></First_Name>
                    <Former_Last_Name></Former_Last_Name>
                    <Email_Address></Email_Address>
                    <Skill></Skill>
                </row>
            </xsl:for-each> 
        </data>
    </xsl:template>
</xsl:stylesheet>

这篇关于通过XSLT将Congnos 10 XML数据转换为更容易遍历XML文件的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆