簡體   English   中英

使用XSLT從XML刪除節點

[英]Remove Nodes from XML with 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>

要使用XSLT“刪除節點”,只需將除那些節點以外的所有內容復制到輸出中。 如果您要丟棄<Address>節點及其內容,則示例XSLT ALMOST會做正確的事情。 您所缺少的是XML輸入文檔的名稱空間。 你需要類似的東西

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

當然,將xmlns:syndication命名空間綁定移至<xsl:stylesheet>元素,並使其繼承,以便在整個樣式表中根據需要都可用前綴,會更干凈一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM