我想使用涉及递归的XSLT转换一些XML。源XML格式为:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Products>
<Product Status="Updated" Type="Both">
<GUID>cca0c671-7070-411d-91e8-895361b53e19</GUID>
<ProductID>48936</ProductID>
<Description>Main Product 1</Description>
<Sequence>357</Sequence>
<LastModifiedTimeStamp>2013-06-06</LastModifiedTimeStamp>
<Parts>
<ProductPart PartType="Individual">
<Name>Individual Part 1-1</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>3</Quantity>
</ProductPart>
<ProductPart PartType="Individual">
<Name>Individual Part 1-2</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>1</Quantity>
</ProductPart>
<ProductPart PartType="SubProduct">
<Name>Sub Product 2</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>1</Quantity>
<SubParts>
<ProductPart PartType="Individual">
<Name>Individual Part 2-1</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>5</Quantity>
</ProductPart>
<ProductPart PartType="Individual">
<Name>Individual Part 2-2</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>10</Quantity>
</ProductPart>
<ProductPart PartType="SubProduct">
<Name>Sub Product 3</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>2</Quantity>
<SubParts>
<ProductPart PartType="Individual">
<Name>Individual Part 3-1</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>6</Quantity>
</ProductPart>
</SubParts>
</ProductPart>
</SubParts>
</ProductPart>
</Parts>
</Product>
</Products>
目标XML:
<Products>
<Product>
<GUID>cca0c671-7070-411d-91e8-895361b53e19</GUID>
<ProductID>48936</ProductID>
<Description>Main Product 1</Description>
<Sequence>357</Sequence>
<LastModifiedTimeStamp>2013-06-06</LastModifiedTimeStamp>
<Subproducts>
<ProductPart PartType="SubProduct">
<Name>Sub Product 2</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>1</Quantity>
<Subproducts>
<ProductPart PartType="SubProduct">
<Name>Sub Product 3</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>2</Quantity>
<Subproducts />
<Subparts>
<ProductPart PartType="Individual">
<Name>Individual Part 3-1</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>6</Quantity>
</ProductPart>
</Subparts>
</ProductPart>
</Subproducts>
<Subparts>
<ProductPart PartType="Individual">
<Name>Individual Part 2-1</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>5</Quantity>
</ProductPart>
<ProductPart PartType="Individual">
<Name>Individual Part 2-2</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>10</Quantity>
</ProductPart>
</Subparts>
</ProductPart>
</Subproducts>
<Subparts>
<ProductPart PartType="Individual">
<Name>Individual Part 1-1</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>3</Quantity>
</ProductPart>
<ProductPart PartType="Individual">
<Name>Individual Part 1-2</Name>
<GUID>a06ac00e-96ed-4c70-8040-6a280bfc8cf2</GUID>
<Quantity>1</Quantity>
</ProductPart>
</Subparts>
</Product>
</Products>
有人可以帮助我使用XSLT吗?我之前没有做过很多关于XML转换的工作,而且迫切需要帮助
修改 我写的XSLT如下:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl">
<xsl:template match='/Products'>
<Products>
<xsl:apply-templates select='Product'/>
</Products>
</xsl:template>
<xsl:template match='Product'>
<Product>
<GUID>
<xsl:value-of select='GUID'/>
</GUID>
<ProductID>
<xsl:value-of select='ProductID'/>
</ProductID>
<Description>
<xsl:value-of select='Description'/>
</Description>
<Sequence>
<xsl:value-of select='Sequence'/>
</Sequence>
<LastModifiedTimeStamp>
<xsl:value-of select='LastModifiedTimeStamp'/>
</LastModifiedTimeStamp>
<Subproducts>
<xsl:apply-templates select='Parts/ProductPart'>
<xsl:with-param name="SubPartParam" select="''" />
</xsl:apply-templates>
</Subproducts>
<Subparts>
<xsl:apply-templates select='Parts/ProductPart'>
<xsl:with-param name="SubPartParam" select="'Yes'" />
</xsl:apply-templates>
</Subparts>
</Product>
</xsl:template>
<xsl:template match='ProductPart'>
<xsl:param name="SubPartParam"/>
<xsl:choose>
<xsl:when test="($SubPartParam='' and @IngredientType='SubProduct')">
<ProductPart>
<xsl:attribute name='PartType'>
<xsl:value-of select='@PartType'/>
</xsl:attribute>
<xsl:apply-templates select='ProductPart' />
<Name>
<xsl:value-of select='Name'/>
</Name>
<GUID>
<xsl:value-of select='GUID'/>
</GUID>
<Quantity>
<xsl:value-of select='Quantity'/>
</Quantity>
</ProductPart>
</xsl:when>
<xsl:when test="($SubPartParam='Yes' and @IngredientType='Individual')">
<ProductPart>
<xsl:attribute name='PartType'>
<xsl:value-of select='@PartType'/>
</xsl:attribute>
<xsl:apply-templates select='ProductPart' />
<Name>
<xsl:value-of select='Name'/>
</Name>
<GUID>
<xsl:value-of select='GUID'/>
</GUID>
<Quantity>
<xsl:value-of select='Quantity'/>
</Quantity>
</ProductPart>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
这个样式表就像你问的那样。这很简单,只需按照不同的顺序复制每个ProductPart
和Product
的{{1}}元素。
困难的部分是从 in 和 out XML数据集中推导出一条规则。如果你用英语描述了你想要的东西,那将会有很大的帮助。
我希望这会有所帮助。输出与“目标XML”相同。
ProductPart