给出以下XML文档:
<?xml version="1.0" encoding="utf-8"?>
<Store>
<Location>
<State>WA</State>
</Location>
<Transaction>
<Category>Fruit</Category>
</Transaction>
<Customer>
<Category>Rewards</Category>
</Customer>
<Document>
<!-- Huge XML blob here -->
</Document>
</Store>
我将如何编写XSLT(版本1或2)将其转换为以下XML文档:
<?xml version="1.0" encoding="utf-8"?>
<Transaction>
<Account>
<Type>Rewards</Type>
</Account>
<Type>
<Department>Fruit</Department>
</Type>
<Document>
<!-- Huge XML blob here -->
</Document>
</Transaction>
?
基本上,我需要重新排列/重命名某些元素,删除某些元素,并复制某些元素,就像它们在原始图中一样。
答案 0 :(得分:1)
您可以使用以下XSLT-1.0样式表/模板来实现您的目标:
<xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="Store">
<Transaction>
<Account> <!-- The Store/Customer/Category is moved to Transaction/Account/Type. -->
<Type>
<xsl:value-of select="Customer/Category" />
</Type>
</Account>
<Type> <!-- The Store/Transaction/Fruit element is moved/renamed to Transaction/Type/Department. -->
<Department>
<xsl:value-of select="Transaction/Category" />
</Department>
</Type>
<product>Rasberries</product> <!-- Adding a new element with a constant value -->
<xsl:copy-of select="Document" /> <!-- The Store/Document element is copied along with all of its sub elements unchanged into the result as the Transaction/Document element. -->
</Transaction>
</xsl:template>
</xsl:stylesheet>
将删除“存储/位置/状态”元素值。
这是不提的。