我有一个像
XML Input 这样的输入 xml:
<parent>
<child>
<child2 name="Action">Change</child2>
<child1 name="Change">
<child2 name="Dest_Author">Author1</child2>
<child2 name="Book1">BookID1</child2>
<child2 name="Type1">General</child2>
<child2 name="Pages1">100</child2>
<child2 name="Book2">BookID2</child2>
<child2 name="Type2">Cooking</child2>
<child2 name="Pages2">200</child2>
<child2 name="Orig_Author">Author2</child2>
<child2 name="Book1">BookID3</child2>
<child2 name="Type1">General</child2>
<child2 name="Pages1">150</child2>
<child2 name="Book2">BookID4</child2>
<child2 name="Type2">Cooking</child2>
<child2 name="Pages2">120</child2>
</child1>
</child>
</parent>
我想使用 xslt 转换将其转换为以下 xml
预期的 XML 输出:
<list>
<author id="Author1">
<book>
<id>BookID1</id>
<type>General</type>
<pages>100</pages>
</book>
<book>
<id>BookID1</id>
<type>Cooking</type>
<pages>200</pages>
</book>
</author>
<author id="Author2">
<book>
<id>BookID1</id>
<type>General</type>
<pages>150</pages>
</book>
<book>
<id>BookID1</id>
<type>Cooking</type>
<pages>120</pages>
</book>
</author>
</list>
我尝试使用 xslt 对其进行转换,但无法这样做。我的 xslt 如下所示
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="parent">
<list>
<xsl:apply-templates />
</list>
</xsl:template>
<xsl:template match="child">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="child1">
<xsl:choose>
<xsl:when test="./@name='Change'">
<author>
<xsl:attribute name="id"><xsl:value-of
select="./child2[@name='Dest_Author']" /></xsl:attribute>
<xsl:apply-templates />
</author>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="child2">
<xsl:choose>
<xsl:when test="contains(@name,'Orig_Author')">
<!-- I thought of changing the below code(line no 36-38) as
</author>
<author>
<xsl:attribute name="id"><xsl:value-of select="text()"/></xsl:attribute>
but if do this it throws error "The element type "xsl:when" must be terminated by the matching end-tag "</xsl:when>"."
-->
<author>
<xsl:attribute name="id"><xsl:value-of select="text()"/></xsl:attribute>
</author>
</xsl:when>
<xsl:when test="contains(@name,'Book')">
<book>
<id>
<xsl:value-of select="text()" />
</id>
<xsl:variable name="next"
select="./following-sibling::node()[@name!=''][1]"></xsl:variable>
<xsl:choose>
<xsl:when test="contains($next/@name,'Type')">
<type>
<xsl:value-of select="$next" />
</type>
<pages>
<xsl:value-of select="$next/following-sibling::node()[@name!=''][1]" />
</pages>
</xsl:when>
<xsl:when test="contains($next/@name,'Pages')">
<pages>
<xsl:value-of select="$next" />
</pages>
</xsl:when>
</xsl:choose>
</book>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
XSLT 输出:
<list>
<author id="Author1">
<book>
<id>BookID1</id>
<type>General</type>
<pages>100</pages>
</book>
<book>
<id>BookID2</id>
<type>Cooking</type>
<pages>200</pages>
</book>
<author id="Author2"/>
<book>
<id>BookID3</id>
<type>General</type>
<pages>150</pages>
</book>
<book>
<id>BookID4</id>
<type>Cooking</type>
<pages>120</pages>
</book>
</author>
</list>
有人可以帮助我并告诉 xslt 代码需要进行哪些更改才能获得正确的输出