Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I do this...(Newbie question)

Status
Not open for further replies.

1ooo1

Technical User
Jan 20, 2003
50
AU
Hi,
I am pretty new to XML and was wondering how I would go about doing this.....

THIS IS AN EXTRACT OF WHAT I HAVE GOT AT PRESENT


<?xml version=&quot;1.0&quot;?>
<BatchRequest004>
<header>
<system>CCeS</system>
<timestamp>20040228124525</timestamp>
<userid>NWAR-X004</userid>
<batchInfo>March 2004 validation</batchInfo>
</header>
<statusRqst>
<crn>206095476A</crn>
<name>AUBREY</name>
<Ass>10016</Ass>
</statusRqst>
<statusRqst>
<crn>205103573T</crn>
<name>HOLMES</name>
<Ass>10017</Ass>
</statusRqst>
<statusRqst>
<crn>205993041B</crn>
<name>HARRADINE</name>
<Ass>10022</Ass>
</statusRqst>

I WANT IT TO LOOK LIKE THIS...

<?xml version=&quot;1.0&quot;?>
<BatchRequest004>
<header>
<system>CCeS</system>
<timestamp>20040228124525</timestamp>
<userid>NWAR-X004</userid>
<batchInfo>March 2004 validation</batchInfo>
</header>
<statusRqst id=&quot;10016&quot;>
<crn>206095476A</crn>
<name>AUBREY</name>
</statusRqst>
<statusRqst id=&quot;10017&quot;>
<crn>205103573T</crn>
<name>HOLMES</name>
</statusRqst>
<statusRqst id=&quot;10022&quot;>
<crn>205993041B</crn>
<name>HARRADINE</name>
</statusRqst>

THE ID NUMBERS INSERTED IN <statusRqst id=&quot;xxxxx&quot;> ARE NOT CONSECUTIVE NUMBERS BUT ARE DERIVED AS IN THE ORIGINAL EXTRACT AS ABOVE...there over 6000 records, so it's a bot of a task to do it manually..
The editor that I am using is XMLShell v1.5
Any assistance would be greatly appreciated.

Ken
 
Here is the XSLT that will do the job. Good luck.

<xsl:stylesheet xmlns:xsl=&quot; version=&quot;1.0&quot;>
<xsl:eek:utput method=&quot;xml&quot; indent=&quot;no&quot; omit-xml-declaration=&quot;yes&quot;/>

<xsl:template match=&quot;/&quot;>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match=&quot;BatchRequest004&quot;>
<BatchRequest004>
<xsl:apply-templates/>
</BatchRequest004>
</xsl:template>

<xsl:template match=&quot;header&quot;>
<header>
<system><xsl:value-of select=&quot;system&quot;/></system>
<timestamp><xsl:value-of select=&quot;timestamp&quot;/></timestamp>
<userid><xsl:value-of select=&quot;userid&quot;/></userid>
<batchInfo><xsl:value-of select=&quot;batchInfo&quot;/></batchInfo>
</header>
</xsl:template>

<xsl:template match=&quot;statusRqst&quot;>
<statusRqst id=&quot;{Ass}&quot;>
<crn><xsl:value-of select=&quot;crn&quot;/></crn>
<name><xsl:value-of select=&quot;name&quot;/></name>
</statusRqst>
</xsl:template>
</xsl:stylesheet>
 
Cool, Manitoba.
I came up with a more generic one, so it will also work for BatchRequest001, or more nodes in statusRqst:

Code:
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;>[/URL]
  <xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/>

  <xsl:template match=&quot;/&quot;>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match=&quot;*&quot;>
    <xsl:choose>

      <xsl:when test=&quot;name()='Ass'&quot;/>

      <xsl:when test=&quot;name()='statusRqst'&quot;>
        <xsl:element name=&quot;statusRqst&quot;>
          <xsl:attribute name=&quot;id&quot;>
            <xsl:value-of select=&quot;Ass&quot;/>
          </xsl:attribute>
          <xsl:apply-templates select=&quot;*&quot;/>
        </xsl:element>
      </xsl:when>

      <xsl:otherwise>
        <xsl:element name=&quot;{name()}&quot;>
          <xsl:choose>
            <xsl:when test=&quot;count(*)=0&quot;>
              <xsl:value-of select=&quot;.&quot;/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select=&quot;*&quot;/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:element>
      </xsl:otherwise>

    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
 
Another option is to use xsl:copy and xsl:copy-of
Code:
<xsl:stylesheet xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL] version=&quot;1.0&quot;>
    <xsl:output method=&quot;xml&quot; indent=&quot;yes&quot; omit-xml-declaration=&quot;no&quot;/>

    <xsl:template match=&quot;BatchRequest004&quot;>
        <xsl:copy>
            <xsl:copy-of select=&quot;//header&quot; />
            <xsl:apply-templates select=&quot;//statusRqst&quot;/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match=&quot;statusRqst&quot;>
        <statusRqst id=&quot;{Ass}&quot;>
            <xsl:copy-of select=&quot;crn&quot;/>
            <xsl:copy-of select=&quot;name&quot;/>
        </statusRqst>
    </xsl:template>
</xsl:stylesheet>
 
Guess we now covered all possibilities?
(I like the copy-of best)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top