我在之前的帖子的帮助下整理了一个XSL来转换一些XML。 我现在有XSL输出正确的数据,但我有两个问题。 1)我有一些额外的文字显示,我不希望在每个标签之前,例如:
BUUBIF
BUBSBAT02
USBIFA
2)我的循环不太正确。标签应该封装内容的每次迭代
<CourseCode/>
<Version/>
<EndDate/>
<PrimaryDisciplineCode/>
<StartDate/>
<Description/>
<FacultyCode/>
我的XML有一些课程数据循环通过两个相关记录然后到下一个课程,所以就实体而言,它应该循环通过VCO,CBK,CRS,然后重新开始。
我的XML:
<?xml version="1.0" encoding="UTF-8" ?>
<exchange>
<vco>
<vco.srs>
<vco_crsc.vco.srs>BUUBIF</vco_crsc.vco.srs>
<vco_prgc.vco.srs>BUBSBAT02</vco_prgc.vco.srs>
<vco_rouc.vco.srs>USBIFI</vco_rouc.vco.srs>
<cbo>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>FJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>FJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
</cbo>
</vco.srs>
<vco.srs>
<vco_crsc.vco.srs>BUUBIF</vco_crsc.vco.srs>
<vco_prgc.vco.srs>BUBSBAT02</vco_prgc.vco.srs>
<vco_rouc.vco.srs>USBIFA</vco_rouc.vco.srs>
<cbo>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>FJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>AJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
<cbo.srs>
<cbo_occl.cbo.srs>FJ</cbo_occl.cbo.srs>
<crs>
<crs.srs>
<crs_titl.crs.srs>BSc Banking & Int Finance</crs_titl.crs.srs>
<crs_dptc.crs.srs>B</crs_dptc.crs.srs>
<crs_begd.crs.srs>2003-08-01</crs_begd.crs.srs>
</crs.srs>
</crs>
</cbo.srs>
</cbo>
</vco.srs>
</vco>
</exchange>
我的XSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/exchange">
<ImportTask>
<CourseVersions>
<CourseVersion>
<xsl:apply-templates select="*|@*"/>
</CourseVersion>
</CourseVersions>
</ImportTask>
</xsl:template>
<xsl:template match="cbo_occl.cbo.srs">
<CourseCode>
<!-- select all text-Nodes of vco_* elements -->
<xsl:for-each select="../../../*[matches(name(),'^vco_.*')]/text()">
<xsl:value-of select="concat(.,'-')"/>
</xsl:for-each>
<xsl:value-of select="."/>
</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
</xsl:template>
<xsl:template match="crs/crs.srs">
<xsl:for-each select="exchange/crs/crs.srs">
</xsl:for-each>
<StartDate><xsl:value-of select="crs_begd.crs.srs"/></StartDate>
<Description><xsl:value-of select="crs_titl.crs.srs"/></Description>
<FacultyCode><xsl:value-of select="crs_dptc.crs.srs"/></FacultyCode>
</xsl:template>
</xsl:stylesheet>
所以这给了我输出:
<?xml version="1.0" encoding="UTF-8"?>
<ImportTask>
<CourseVersions>
<CourseVersion>
BUUBIF
BUBSBAT02
USBIFI
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-FJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFI-FJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
BUUBIF
BUBSBAT02
USBIFA
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-FJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-AJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
<CourseCode>BUUBIF-BUBSBAT02-USBIFA-FJ</CourseCode>
<Version>1</Version>
<EndDate>i:nil="true"</EndDate>
<PrimaryDisciplineCode>i:nil="true"</PrimaryDisciplineCode>
<StartDate>2003-08-01</StartDate>
<Description>BSc Banking & Int Finance</Description>
<FacultyCode>B</FacultyCode>
</CourseVersion>
</CourseVersions>
</ImportTask>
非常感谢
约拿
答案 0 :(得分:2)
使用<xsl:apply-templates select="*|@*"/>
处理所有属性和子节点,这意味着例如<vco_crsc.vco.srs>BUUBIF</vco_crsc.vco.srs>
由最终输出所有文本节点的内置模板处理。
因此,要么添加<xsl:template match="text()"/>
以避免文本节点创建输出,要么更改apply-templates
以仅处理您为其编写模板的两个元素:<xsl:apply-templates select="//cbo_occl.cbo.srs | //crs/crs.srs"/>
。