是演示xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="TrialDemoModify3.xsl"?>
<class>
<students>
<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>
</class>
MY xsl 我正在关注演示xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"></xsl:output>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()" />
<xsl:if test="self::navigation">
<node title="Employees">
<node>
<xsl:apply-templates select="node()" mode="group" />
</node>
</node>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template>
<xsl:choose>
<xsl:when test="@lastname=*">
<xsl:copy>
<xsl:copy-of select="lastname"/>
<xsl:apply-templates select="node()" mode="group"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="node()" mode="group" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="group" />
我的预期输出应该与以下xml类似,但不是这样:
<class>
<students>
<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>
<Employees>
<Employee><lastname>Kad</lastname></Employee>
<Employee><lastname>Gupta</lastname></Employee>
<Employee><lastname>Singh</lastname></Employee>
</Employees>
</class>
我已经在这段代码上度过了最后三天。这只是一个演示代码,但即便如此也是如此。 请帮助......我无法获得预期的输出 ....而是我的 xml保持原样。 感谢
答案 0 :(得分:0)
这是快速回答。
使用以下XSLT
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*/*">
<class>
<xsl:copy-of select="."/>
<Employees>
<xsl:for-each select="/class/students/student/lastname">
<Employee>
<xsl:copy-of select="."/>
</Employee>
</xsl:for-each>
</Employees>
</class>
</xsl:template>
</xsl:stylesheet>
生成以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<class>
<students>
<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>
</students>
<Employees>
<Employee>
<lastname>Kad</lastname>
</Employee>
<Employee>
<lastname>Gupta</lastname>
</Employee>
<Employee>
<lastname>Singh</lastname>
</Employee>
</Employees>
</class>