我有一個輸入XML轉換給定的XML到另一個XML像
XML輸入:使用XSLT
<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>
我想將其轉換爲下面的XML使用XSLT轉換
預期的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代碼需要來獲得正確的輸出
你的XSLT是什麼樣的?而不是給你的代碼,它可能是你的代碼不遠處。 –
歡迎來到堆棧溢出!看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出和實際獲得的輸出(控制檯輸出,堆棧跟蹤,編譯器錯誤 - 無論是適用)。您提供的詳細信息越多,您可能收到的答案越多 – dhein
我已經添加了我的xslt – SAM