首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSL转换,请求子节点数据

XSL转换,请求子节点数据
EN

Stack Overflow用户
提问于 2014-04-18 13:02:08
回答 1查看 115关注 0票数 1

我刚刚开始使用XSL转换--为了在我的项目中添加额外的抽象层,我想使用它们。

我希望使用转换将水晶报告生成的XML文件转换为另一个XML,为我的项目简化(因此,将来,当文件的模式发生变化时,我只需要更改XSL文件)。

因此,我的输入XML文件如下所示:

代码语言:javascript
复制
<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail"  
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">
    <Group Level="1">
        <GroupHeader>
            <Section SectionNumber="0">
                <Field Name="Field4" FieldName="{@PartIndex}"><FormattedValue>Part Number</FormattedValue><Value>51-01672</Value></Field>
            </Section>
            <Section SectionNumber="1">
                <Text Name="Text28"><TextValue>Part Description</TextValue>
                </Text>
            </Section>
            <Section SectionNumber="2">
                <Text Name="Text21"><TextValue>Part Description 2</TextValue>
                </Text>
            </Section>
            <Section SectionNumber="3">
            </Section>
...

这只是其中的一部分。我要提到的是,GroupHeader节点不会在Group节点内部重复。

所以我做了一些早期的XSL定义(为了测试的缘故):

代码语言:javascript
复制
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:t="urn:crystal-reports:schemas:report-detail"  
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/t:CrystalReport">
    <ToolsUsage>
        <xsl:value-of select="t:Group[1]/@Level"/>
    </ToolsUsage>
</xsl:template>

</xsl:stylesheet>

我使用lxml Python来测试它。

如预期的那样,返回以下代码:

代码语言:javascript
复制
<ToolsUsage xmlns:t="urn:crystal-reports:schemas:report-detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1</ToolsUsage>

到目前为止,还不错(对于第一组节点,级别属性等于"1“)。但我不能再往前走了。

类似地,我试图获取Field节点的内部文本的值(第一部分出现):

代码语言:javascript
复制
<xsl:value-of select="t:Group[1]/GroupHeader/Section[1]/Field/Value"/>

但是我没有得到任何信息(因此在节点中)。我试图获得一个SectionNumber属性,但也没有结果。我甚至使用XPath工具来提取确切的XPath查询,但这些查询似乎是正确的。我相信这是非常基本的东西,但我不知道是什么。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-18 13:11:36

给定<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail">...</CrystalReport>,所有子代元素都在名称空间中,因此您的路径需要在所有元素(例如t:Group[1]/t:GroupHeader/t:Section[1]/t:Field/t:Value )上使用前缀。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23154738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档