XLS转换,请求子节点d

2024-04-20 04:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚开始使用XLS转换—我想使用它们在我的项目中添加额外的抽象层。 我想使用转换将crystalreports生成的xml文件转换成另一个xml文件,为我的项目简化(因此,在将来,当文件的模式发生更改时,我只需要更改xsl文件)。你知道吗

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

<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节点在组节点内部不重复。你知道吗

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

<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 lib来测试它。 正如预期的那样,这将返回以下代码:

<ToolsUsage xmlns:t="urn:crystal-reports:schemas:report-detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1</ToolsUsage>

到目前为止,还不错(对于第一个组节点,Level属性等于“1”)。但我不能再往前走了。 例如,我尝试获取字段节点的值内部文本(出现在第一节):

<xsl:value-of select="t:Group[1]/GroupHeader/Section[1]/Field/Value"/>

但是我没有得到任何东西(结果是在node中)。我尝试获取SectionNumber属性,但也没有结果。我甚至使用xmlpath工具来提取精确的xpath查询,但看起来,查询是正确的。我相信这是很基本的东西,但找不到什么。你知道吗


Tags: 文件reporthttpwwwsectionxmlschemascrystal
1条回答
网友
1楼 · 发布于 2024-04-20 04:01:47

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

相关问题 更多 >