【问题标题】:XML - Help removing nodes being publishedXML - 帮助删除正在发布的节点
【发布时间】:2011-05-03 22:10:37
【问题描述】:

对此非常感谢。

我想从外部 xml 文件的提要中删除某些节点,该文件已使用 xslt 设置样式。这是提要:http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20

我要删除的节点是:

本地活动发布到 WhereCanWeGo.com.00392CM15 9EH31/10/1007/11/101000111111111111111111031 十月 2010http://www.wherecanwego.com/events/signin.aspxww.wherecanwego.com/events/signin.aspx

谁能指导我如何删除这些初始节点(参数)?它们是邮政编码、帐号、Feed URL 等。

我很想完成这件事,但这是最后的障碍!非常感谢任何回复的人......

样式表(片段)

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

【问题讨论】:

    标签: asp.net xml xslt


    【解决方案1】:

    如果您已经使用的 XSLT 样式表不能完全满足您的要求,请发布该样式表或指向它的链接。 [编辑] 在您发布的样式表更改中

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

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

    【讨论】:

    • 嗨,马丁,当然感谢您的提问 - 我不想让任何人感到厌烦,所以尽量保持简洁!这是我的 XSLT,是的,我很想知道如何确保不发布顶级子节点(在“项目”之上):我现在似乎无法在这里发布他的代码!我会看看如何链接到它...
    【解决方案2】:

    从您提出的问题和分析提要来看,您似乎想要摆脱顶部 (LocalEvents) 节点中未命名为 item 的所有子节点。

    这种转变

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

    当应用于提供的提要 (http://www.wcwg.info/feeds/localevents.aspx?a=00392&p=CM159EH&m=20) 时,会产生想要的结果

    注意:需要的元素如何被匹配它们的空模板删除(不处理,忽略)并覆盖用于复制其余部分的身份规则的节点“原样”。

    【讨论】:

    • 真的有点粗鲁,我不认为它执行得很差。
    • @sue:即使是现在,@Alejandro 做了很多工作让它变得更好,它还缺少: 1. 源 XML 文档; 2. 不需要水平滚动的正确格式。在 Alejandro 执行编辑之前,XSLT 代码根本不可见。简而言之——一个典型的例子,如何提出问题。
    【解决方案3】:

    现在已更正为以下内容,现在适用于处于相同情况的其他任何人:

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

    【讨论】:

      【解决方案4】:

      其他方法。这个样式表:

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

      注意:在同一层次结构中进行一对一转换时,只需声明输入源中特定元素的规则并覆盖文本节点的内置规则(输出字符串值),规则为空。

      【讨论】:

        猜你喜欢
        • 2011-02-09
        • 1970-01-01
        • 1970-01-01
        • 2022-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多