我有一个输入XML,如下所示。我想删除空标签,根据特定的子密钥对父节点进行排序,并且只提取51到100之间的父节点及其所有子节点C1到CN。
我在下面的XSLT中得到了XML解析器错误:
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<class>
<dept>
<name>chemistry></name>
<code>C001</code>
<section>C1</section>
<bldg>B</bldg>
</dept>
<student rollno="393">
<firstname>Dinkar</firstname>
<lastname>Kad</lastname>
<nickname>Dinkar</nickname>
<marks>85</marks>
</student>
<student rollno="493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>Vinni</nickname>
<marks>95</marks>
</student>
<student rollno="593">
<firstname>Jasvir</firstname>
<lastname>Singh</lastname>
<nickname>Jazz</nickname>
<marks>90</marks>
</student>
<student rollno="105">
<firstname>Jasvir1</firstname>
<lastname>Singh1</lastname>
<nickname>Jazz1</nickname>
<marks>90</marks>
</student>
<student rollno="102">
<firstname>Jasvir2</firstname>
<lastname>Singh2</lastname>
<nickname>Jazz2</nickname>
<marks>95</marks>
</student>
<noOfStud>3</noOfStud>
</class>
XSLT:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" version="1.0" encoding="UTF-8" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:if test=".!=''">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates
select="node()[boolean(normalize-space())]
|@*">
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="C1" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="/root/P1[position() >= 51 and position() <= 100]" />
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
我猜你想做:
<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:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/root">
<xsl:copy>
<!-- extract only the parent nodes from 51 to 100 -->
<xsl:apply-templates select="*[position() >= 51 and position() <= 100]">
<!-- sort the parent nodes based on a specific child -->
<xsl:sort select="C1" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- remove the empty tags -->
<xsl:template match="*[not(normalize-space())]"/>
</xsl:stylesheet>
在修改过的示例中,您应该使第二个模板类似于:
<xsl:template match="/class">
<xsl:copy>
<xsl:apply-templates select="dept"/>
<!-- extract only the student nodes from 51 to 100 -->
<xsl:apply-templates select="student[position() >= 51 and position() <= 100]">
<!-- sort the student nodes based on ??? -->
<xsl:sort select="some-node" data-type="number" order="ascending"/>
</xsl:apply-templates>
<xsl:apply-templates select="noOfStud"/>
</xsl:copy>
</xsl:template>