我需要为网站服务创建一个XML文档,通过搜索引擎显示许多电子商店的产品。
当我与开发团队交谈时,他们说我需要一个遵循他们的结构和系统的XML。
我的问题是,如何在eshop.xml的基础上创建一个新的eshopNEW.xml,但更改一些元素的名称以满足其结构要求?例如,假设我需要将item
更改为product
,将title
更改为name
,并在其中添加属性type="game"
。
我粘贴了一个简单的XML,这样我就可以更容易地理解创建新文档的过程和名称的转换。
谢谢你的回答。
<item>
<title>Hide your heart</title>
<quantity>1</quantity>
<price>9.90</price>
</item>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
听起来是XSLT的一个很好的用例。
例如,放入一个身份模板,默认情况下通过未更改的元素:
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
然后为需要更改名称的元素添加特定模板:
<xsl:template match="item">
<product>
<xsl:apply-templates select="node() | @*" />
</product>
</xsl:template>
<xsl:template match="title">
<name>
<xsl:apply-templates select="node() | @*" />
</name>
</xsl:template>