开发者

How can I create a new XML doc based on my existing one and rename some elements?

开发者 https://www.devze.com 2023-03-14 17:46 出处:网络
I need to create an XML document for a website service that shows products from many e-shops through their search engine.

I need to create an XML document for a website service that shows products from many e-shops through their search engine.

When I talked with the development team they said me that I need to have an XML that follows their structure and system.

My question is how can I create a new eshopNEW.xml based on my eshop.xml BUT change the names of some of the elements to meet their structure requirements? For example suppose that I need to change the item to product, title to name and add into it an attribute type="game".

I have pasted a simple XML so that I will understand the process of creating a new document and the transformation of the names easier.

Thank you for your answers.

  <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开发者_如何学Go</quantity>
    <price>10.90</price>
  </item>


Sounds like a good use case for XSLT.

E.g. put in an identity template to pass elements through unchanged by default:

<xsl:template match="node() | @*">
  <xsl:copy>
    <xsl:apply-templates select="node() | @*" />
  </xsl:copy>
</xsl:template>

Then add specific templates for elements whose names you need to change:

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消