使用XSLT从XML中删除节点

时间:2014-03-07 17:09:28

标签: xml xslt

我有以下示例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>agent.lead.email@listhub.net</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>

从这个XML文件中,我想使用XSLT删除特定节点,最后使用代理,经纪,列表,照片和参与者。

这意味着,我想删除列表节点的部分示例。

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

我一直在搞乱这个XSLT试图获得它,但它不起作用..

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

1 个答案:

答案 0 :(得分:1)

要使用XSLT“删除节点”,只需将除这些节点之外的所有内容复制到输出中。如果要丢弃<Address>个节点及其内容,那么您的示例XSLT ALMOST会做正确的事情。您错过的是XML输入文档是命名空间。你需要像

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

当然,将xmlns:syndication命名空间绑定到<xsl:stylesheet>元素会更加清晰,并让它继承,以便在整个样式表中根据需要提供前缀。

相关问题