我刚刚开始使用XSL转换--为了在我的项目中添加额外的抽象层,我想使用它们。
我希望使用转换将水晶报告生成的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节点不会在Group节点内部重复。
所以我做了一些早期的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来测试它。
如预期的那样,返回以下代码:
<ToolsUsage xmlns:t="urn:crystal-reports:schemas:report-detail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">1</ToolsUsage>到目前为止,还不错(对于第一组节点,级别属性等于"1“)。但我不能再往前走了。
类似地,我试图获取Field节点的内部文本的值(第一部分出现):
<xsl:value-of select="t:Group[1]/GroupHeader/Section[1]/Field/Value"/>但是我没有得到任何信息(因此在节点中)。我试图获得一个SectionNumber属性,但也没有结果。我甚至使用XPath工具来提取确切的XPath查询,但这些查询似乎是正确的。我相信这是非常基本的东西,但我不知道是什么。
发布于 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 )上使用前缀。
https://stackoverflow.com/questions/23154738
复制相似问题