开发者

XML - Help removing nodes being published

Any help much appreciated on this.

I'd like to remove certain nodes from being published in a feed from an external xml file, which I've styled with xslt. Here is the feed:http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20.

The nodes I would like to remove are:

Local Events posted to WhereCanWeGo.com.00392CM15 9EH31/10/1007/11/1010001111111111111111111031 October 2010http://www.wherecanwego.com/events/signin.aspxww.wherecanwego.com/events/signin.aspx

Could anybody be kind enough to direct me how to remove these initial nodes (parameters)? They are the postcode, account number, feed URL etc.

I'm desperate to get this finished, but it's the final hurdle! Many thanks in advance for anyone who responds...

The stylesheet (fragment)

<xsl:template match="/"> 
  <xsl:apply-templates/>   
</xsl:template> 

 <xsl:template match="item"> 
  <div class="local_events"> 
    <xsl:apply-templates select="title"/>   
    <xsl:apply-templates select="Venue"/> 
    <xsl:apply-templates select="Times"/> 
    <xsl:apply-开发者_开发百科templates select="Dates"/> 
    <xsl:apply-templates select="DetailsURL"/> 
  </div><div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div> 
 </xsl:template> 

<xsl:template match="title"> 
  <h2><a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}"><xsl:value-of select="."/></a></h2> 
</xsl:template> 

<xsl:template match="Venue"> 
  <span>Location: </span> 
  <xsl:value-of select="."/> 
  <br /> 
</xsl:template> 

<xsl:template match="Times"> 
  <span>Details: </span> 
  <xsl:value-of select="."/> 
  <br /> 
</xsl:template> 

<xsl:template match="Dates"> 
  <span>Dates: </span> 
  <xsl:value-of select="."/> 
</xsl:template> 

<xsl:template match="DetailsURL"> 
   <a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}"><xsl:text>Full details...</xsl:text></a> 
</xsl:template> 


From your poorly formulated question and analyzing the feed, it seems that you want to get rid of all children of the top (LocalEvents) node that are not named item.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="/*/*[not(self::item)]"/>
</xsl:stylesheet>

when applied on the provided feed (http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20), produces the wanted result.

Do note: how the wanted elements are deleted (not processed, ignored) by an empty template matching them and overriding the identity rule that is used to copy the rest of the nodes "as-is".


If you already use an XSLT stylesheet that does not do exactly what you want then please post the stylesheet or a link to it. [edit] In your posted stylesheet change

<xsl:template match="/">
  <xsl:apply-templates/>  
</xsl:template>

to

<xsl:template match="/">
  <xsl:apply-templates select="//item"/>
</xsl:template>


This has now been corrected to the following which now works, for anyone else in the same situation:

<xsl:template match="/">
  <xsl:apply-templates select="/LocalEvents/item"/>    
</xsl:template>

 <xsl:template match="item">
  <div class="local_events">
    <xsl:apply-templates select="title"/>  
    <xsl:apply-templates select="Venue"/>
    <xsl:apply-templates select="Times"/>
    <xsl:apply-templates select="Dates"/>
    <xsl:apply-templates select="DetailsURL"/>
  </div><div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
 </xsl:template>

<xsl:template match="title">
  <h2><a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}"><xsl:value-of select="."/></a></h2>
</xsl:template>

<xsl:template match="Venue">
  <span>Location: </span>
  <xsl:value-of select="."/>
  <br />
</xsl:template>

<xsl:template match="Times">
  <span>Details: </span>
  <xsl:value-of select="."/>
  <br />
</xsl:template>

<xsl:template match="Dates">
  <span>Dates: </span>
  <xsl:value-of select="."/>
</xsl:template>

<xsl:template match="DetailsURL">
   <a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}"><xsl:text>Full details...</xsl:text></a>
</xsl:template>

</xsl:stylesheet>


Other approuch. This stylesheet:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:preserve-space elements="Times"/>
    <xsl:template match="text()"/>
    <xsl:template match="item">
        <div class="local_events">
            <xsl:apply-templates/>
        </div>
        <div style="height:1px;border-bottom:1px dotted #cfcfcf;"></div>
    </xsl:template>
    <xsl:template match="title">
        <h2>
            <a class="title" target="_blank" rel="nofollow" href="{../DetailsURL}">
                <xsl:value-of select="."/>
            </a>
        </h2>
    </xsl:template>
    <xsl:template match="Venue">
        <span>Location: </span>
        <xsl:value-of select="."/>
        <br />
    </xsl:template>
    <xsl:template match="Times">
        <span>Details: </span>
        <xsl:value-of select="."/>
        <br />
    </xsl:template>
    <xsl:template match="Dates">
        <span>Dates: </span>
        <xsl:value-of select="."/>
    </xsl:template>
    <xsl:template match="DetailsURL">
        <a style="font-weight:normal;margin-left:0.5em;" target="_blank" rel="nofollow" href="{.}">
            <xsl:text>Full details...</xsl:text>
        </a>
    </xsl:template>
</xsl:stylesheet>

Note: When doing a one to one transformation in the same hierarchy, it's enough to declare the rules for specific elements in input source and to overwrite the built-in rule for text node (output the string value) with an empty rule.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜