user3393522
user3393522

Reputation: 1

Remove Nodes from XML with XSLT

I've got the following example XML..

http://app.listhub.com/syndication-docs/example.xml

    <Listings xmlns="http://rets.org/xsd/Syndication/2012-03" xmlns:commons="http://rets.org/xsd/RETSCommons"      xmlns:schemaLocation="http://rets.org/xsd/Syndication/2012-03/Syndication.xsd"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" listingsKey="2012-03-06T22:14:47"     version="0.96" versionTimestamp="2012-02-07T03:00:00Z" xml:lang="en-us">
    <Listing>
        <Address>...</Address>
        <ListPrice commons:isgSecurityClass="Public">234000</ListPrice>
        <ListPriceLow commons:isgSecurityClass="Public">214000</ListPriceLow>
        <AlternatePrices>...</AlternatePrices>
        <ListingURL>http://www.somemls.com/lisings/1234567890</ListingURL>
        <ProviderName>SomeMLS</ProviderName>
        <ProviderURL>http://www.somemls.com</ProviderURL>
        <ProviderCategory>MLS</ProviderCategory>
        <LeadRoutingEmail>[email protected]</LeadRoutingEmail>
        <Bedrooms>3</Bedrooms>
        <Bathrooms>8</Bathrooms>
        <PropertyType otherDescription="Ranch">Commercial</PropertyType>
        <PropertySubType otherDescription="Ranch">Apartment</PropertySubType>
        <ListingKey>3yd-SOMEMLS-1234567890</ListingKey>
        <ListingCategory>Purchase</ListingCategory>
        <ListingStatus>Active</ListingStatus>
        <MarketingInformation>...</MarketingInformation>
        <Photos>...</Photos>
        <DiscloseAddress>true</DiscloseAddress>
        <ListingDescription>...</ListingDescription>
        <MlsId>SOMEMLS</MlsId>
        <MlsName>Listing Exchange Group</MlsName>
        <MlsNumber>1234567890</MlsNumber>
        <LivingArea>2200</LivingArea>
        <LotSize>130680.000000</LotSize>
        <YearBuilt>1992</YearBuilt>
        <ListingDate>2012-01-06</ListingDate>
        <ListingTitle>Ranch, Ranch - Morgantown, WV</ListingTitle>
        <FullBathrooms>2</FullBathrooms>
        <ThreeQuarterBathrooms>3</ThreeQuarterBathrooms>
        <HalfBathrooms>2</HalfBathrooms>
        <OneQuarterBathrooms>1</OneQuarterBathrooms>
        <ForeclosureStatus>REO - Bank Owned</ForeclosureStatus>
        <ListingParticipants>...</ListingParticipants>
        <VirtualTours>...</VirtualTours>
        <Videos>...</Videos>
        <Offices>...</Offices>
        <Brokerage>...</Brokerage>
        <Franchise>...</Franchise>
        <Builder>...</Builder>
        <Location>...</Location>
        <OpenHouses>...</OpenHouses>
        <Taxes>...</Taxes>
        <Expenses>...</Expenses>
        <DetailedCharacteristics>...</DetailedCharacteristics>
        <ModificationTimestamp commons:isgSecurityClass="Public">2012-03-06T17:14:47-   05:00</ModificationTimestamp>
    </Listing>
</Listings>

From this XML file i want to use XSLT to remove particular nodes and end up with Agents, Brokerages, Listings, Photos and Participants.

This means, I would want to delete for example parts of the listing node.

<Listing> 
  <MarketingInfomation>
  <VirtualTour>
  <Videos>
  <Franchise>
  <Taxes>
  <Expenses>

I've been messing with this XSLT trying to get it but it doesn't work..

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://rets.org/xsd/Syndication/2012-03"version="1.0">
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="xmlns:Address"/>
</xsl:stylesheet>

Upvotes: 0

Views: 376

Answers (1)

keshlam
keshlam

Reputation: 8058

To "remove nodes" with XSLT, simply copy everything except those nodes to the output. Your sample XSLT ALMOST does the right thing if you want to discard <Address> nodes and their content. What you've missed is that the XML input document is namespaced. You need something like

<xsl:template match="syndication:Address" 
              xmlns:syndication="http://rets.org/xsd/Syndication/2012-03"/>

Of course it would be a bit cleaner to move the xmlns:syndicationnamespace binding up to the <xsl:stylesheet> element, and let it be inherited so that prefix is available as needed throughout your stylesheet.

Upvotes: 1

Related Questions