开发者

Changing tags of a xml based on a dtd

开发者 https://www.devze.com 2023-02-27 03:42 出处:网络
I\'m working with this xml <?xml version=\"1.0\"?> <globalcoverage> <carrier> <Data>CONTINENT</Data>

I'm working with this xml

    <?xml version="1.0"?>
  <globalcoverage>
   <carrier>
    <Data>CONTINENT</Data>
    <Data>COUNTY<开发者_StackOverflow社区/Data>
    <Data>OPERATOR</Data>
    <Data>FREQUENCY</Data>
    <Data>SIGNATURE</Data>
    <Data>GSM</Data>
    <Data>GPRS</Data>
    <Data>3G</Data>
    <Data>PAYMENT</Data>
    <Data>SMS</Data>
    <Data>ZONE</Data>
    <Data>STATUS</Data>
   </carrier>
   <carrier>
    <Data>AFRICA</Data>
    <Data>ZAMBIA</Data>
    <Data>MTN Zambia</Data>
    <Data>GSM 900</Data>
    <Data>2006-08-14</Data>
    <Data>X</Data>
    <Data>X</Data>
    <Data>X</Data>
    <Data>X</Data>
    <Data>X</Data>
    <Data>NA</Data>
    <Data>0</Data>
   </carrier>
...

It has a lot more records, I want to change the tags, but don't want to do it one by one. There is a way to do it with oxygen using this dtd?

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT coverturamundial (item+) >
<!ELEMENT item (continent, county, operator, frequency, signature, gsm, gprs, _3g, payment, sms, zone, status) >
<!ELEMENT continent (#PCDATA) >
<!ELEMENT county (#PCDATA) >
<!ELEMENT operator (#PCDATA) >
<!ELEMENT frequency (#PCDATA) >
<!ELEMENT signature (#PCDATA) >
<!ELEMENT gsm (#PCDATA) >
<!ELEMENT gprs (#PCDATA)>
<!ELEMENT _3g (#PCDATA) >
<!ELEMENT payment (#PCDATA) >
<!ELEMENT sms (#PCDATA) >
<!ELEMENT zone (#PCDATA) >
<!ELEMENT status (#PCDATA)>

In fact, the data was taken from a xls. Thanks for your help.


I don't think that there is an automated way to do this in oXygen. You could do a simple XSLT transform though. It looks like there are always 12 Data elements that correspond to the 12 elements in item in the DTD. If this is true, you can base the conversion of Data elements on their position.

Here's a sample stylesheet (NOTE: I'm outputting a DOCTYPE with a system identifier to easily validate the output in oXygen; you can remove/change this):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"  doctype-system="test.dtd"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="globalcoverage">
    <coverturamundial>
      <xsl:apply-templates/>
    </coverturamundial>
  </xsl:template>

  <xsl:template match="carrier">
    <item>
      <xsl:apply-templates/>
    </item>
  </xsl:template>

  <xsl:template match="Data[1]">
    <continent>
      <xsl:apply-templates/>
    </continent>    
  </xsl:template>
  <xsl:template match="Data[2]">
    <county>
      <xsl:apply-templates/>
    </county>    
  </xsl:template>
  <xsl:template match="Data[3]">
    <operator>
      <xsl:apply-templates/>
    </operator>    
  </xsl:template>
  <xsl:template match="Data[4]">
    <frequency>
      <xsl:apply-templates/>
    </frequency>    
  </xsl:template>
  <xsl:template match="Data[5]">
    <signature>
      <xsl:apply-templates/>
    </signature>    
  </xsl:template>
  <xsl:template match="Data[6]">
    <gsm>
      <xsl:apply-templates/>
    </gsm>    
  </xsl:template>
  <xsl:template match="Data[7]">
    <gprs>
      <xsl:apply-templates/>
    </gprs>    
  </xsl:template>
  <xsl:template match="Data[8]">
    <_3g>
      <xsl:apply-templates/>
    </_3g>    
  </xsl:template>
  <xsl:template match="Data[9]">
    <payment>
      <xsl:apply-templates/>
    </payment>    
  </xsl:template>
  <xsl:template match="Data[10]">
    <sms>
      <xsl:apply-templates/>
    </sms>    
  </xsl:template>
  <xsl:template match="Data[11]">
    <zone>
      <xsl:apply-templates/>
    </zone>    
  </xsl:template>
  <xsl:template match="Data[12]">
    <status>
      <xsl:apply-templates/>
    </status>    
  </xsl:template>

</xsl:stylesheet>

Here's what the output would be using your sample input (modified to make it well formed):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE coverturamundial
  SYSTEM "test.dtd">
<coverturamundial>
   <item>
      <continent>CONTINENT</continent>
      <county>COUNTY</county>
      <operator>OPERATOR</operator>
      <frequency>FREQUENCY</frequency>
      <signature>SIGNATURE</signature>
      <gsm>GSM</gsm>
      <gprs>GPRS</gprs>
      <_3g>3G</_3g>
      <payment>PAYMENT</payment>
      <sms>SMS</sms>
      <zone>ZONE</zone>
      <status>STATUS</status>
   </item>
   <item>
      <continent>AFRICA</continent>
      <county>ZAMBIA</county>
      <operator>MTN Zambia</operator>
      <frequency>GSM 900</frequency>
      <signature>2006-08-14</signature>
      <gsm>X</gsm>
      <gprs>X</gprs>
      <_3g>X</_3g>
      <payment>X</payment>
      <sms>X</sms>
      <zone>NA</zone>
      <status>0</status>
   </item>
</coverturamundial>

If you don't want to do individual xsl:template's for the data elements, you can do something like:

<xsl:template match="Data">
    <xsl:choose>
      <xsl:when test="position()=1">
        <continent>
          <xsl:apply-templates/>
        </continent>
      </xsl:when>
      <xsl:when test="position()=2">
        <county>
          <xsl:apply-templates/>
        </county>
      </xsl:when>
    </xsl:choose>
    ...
  </xsl:template>

Hope this helps.

0

精彩评论

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