Monarch
Monarch

Reputation: 1

How to change XML with XSL

So I have some XML and I want the same XML but with a few lines gone. Here's my original XML :

 <?xml version="1.0" encoding="UTF-8"?>
<Customers>
  <Customer>
    <FirstName>Sarah</FirstName>
    <LastName>Bellum</LastName>
    <PhoneNumbers>
      <PhoneNumber type="HOME">
        223 704 2215
      </PhoneNumber>
      <PhoneNumber type="WORK">
        223 704 1234
      </PhoneNumber>
      <PhoneNumber type="CELL">
        860 704 1234
      </PhoneNumber>
    </PhoneNumbers>
    <Addresses>
      <Address type="HOME">
        <Line1>123 Main</Line1>
        <Line2></Line2>
        <Line3></Line3>
        <City>Wallingford</City>
        <State>CT</State>
        <Zip>12345</Zip>
      </Address>
      <Address type="WOKR">
        <Line1>456 Main</Line1>
        <Line2></Line2>
        <Line3></Line3>
        <City>Willington</City>
        <State>CT</State>
        <Zip>12345</Zip>
      </Address>
    </Addresses>
  </Customer>

  <Customer>
    <FirstName>Mike</FirstName>
    <LastName>Easter</LastName>
    <PhoneNumbers>
      <PhoneNumber type="CELL">
        123 704 4321
      </PhoneNumber>
    </PhoneNumbers>
    <Addresses>
      <Address type="HOME">
        <Line1>14 East</Line1>
        <Line2></Line2>
        <Line3></Line3>
        <City>Shelton</City>
        <State>CT</State>
        <Zip>12345</Zip>
      </Address>
    </Addresses>
  </Customer>
</Customers>

and here is my target XML :

 <Customers>
  <Customer>
    <Name>Sarah Bellum</Name>
    <!-- This element should only appear if the source element exists -->
    <HomePhone>223 704 2215</HomePhone>
    <!-- This element should only appear if the source element exists -->
    <WorkPhone>223 704 1234</WorkPhone>
    <!-- This element should only appear if the source element exists -->
    <CellPhone>860 704 1234</CellPhone>
    <!-- This element should only appear if the source element exists -->
    <HomeAddress>
      <Street>123 Main</Street>
      <City>Wallingford</City>
      <State>CT</State>
      <Zip>12345</Zip>
    </HomeAddress>
    <!-- This element should only appear if the source element exists -->
    <WorkAddress>
      <Street>456 Main</Street>
      <City>Willington</City>
      <State>CT</State>
      <Zip>12345</Zip>
    </WorkAddress>
  </Customer>

  <Customer>
    <Name>Mike Easter</Name>
    <CellPhone>123 704 4321</CellPhone>
    <HomeAddress>
      <Street>14 East</Street>
      <City>Shelton</City>
      <State>CT</State>
      <Zip>12345</Zip>
    </HomeAddress>
  </Customer>
</Customers>

I have some XSLT but it just copies it and I want some lines gone. Here is my XSLT :

 <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

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

So I gave you the target XML and the original and some XSL. I don't know how to get rid of lines so can you pls help.

Upvotes: 0

Views: 40

Answers (1)

Michael Kay
Michael Kay

Reputation: 163595

Well, some of the transformation rules here are easy, for example

<xsl:template match="FirstName">
 <Name><xsl:value-of select="concat(., ' ', ../LastName)"/>
</xsl:template>

<xsl:template match="LastName"/>

or

<xsl:template match="Phone[@type='CELL']">
  <CellPhone><xsl:value-of select="."/></CellPhone>
</xsl:template>

Others are more more difficult; I don't know how you decide which line of the input address is the Street.

But basically, what is your question? What exactly is the difficulty you are facing with this transformation? On the face of it, there isn't anything here that isn't covered in any "Beginning XSLT" textbook.

Upvotes: 1

Related Questions