【问题标题】:xslt help - my transform is not rendering correctlyxslt 帮助 - 我的转换未正确呈现
【发布时间】:2010-05-28 16:47:10
【问题描述】:

我正在尝试使用以下文件应用 xlst 转换。这是非常基本的,但是当我让它正常工作时,我想以此为基础。

 <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:template match="/">
  <div>
  <h2>Station Inventory</h2>
  <hr/>
  <xsl:apply-templates/>
  </div>
</xsl:template>

 <xsl:template match="StationInventory">
   <h5><xsl:value-of select="station-name" /></h5>
   <xsl:apply-templates select="detector"/>
  </xsl:template>


  <xsl:template match="detector">
   <span>
     <xsl:value-of select="detector-name" />
  </span>
  <br/>
  </xsl:template>
 </xsl:stylesheet>

这是我用于源代码的一些 xml。

 <StationInventoryList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns="http://www.dummy-tmdd-address">
  <StationInventory>
   <station-id>9940</station-id>
   <station-name>Zone 9940-SEB</station-name>
   <station-travel-direction>SEB</station-travel-direction>
   <detector-list>
  <detector>
    <detector-id>2910</detector-id>
    <detector-name>1999 West Smith Exit SEB</detector-name>
  </detector>
  <detector>
    <detector-id>9205</detector-id>
    <detector-name>CR-155 Exit SEB</detector-name>
  </detector>
  <detector>
    <detector-id>9710</detector-id>
    <detector-name>Pt of View SEB</detector-name>
  </detector>
  </detector-list>
  </StationInventory>
</StationInventoryList>

任何想法我做错了什么?这里的简单意图是制作一个站点列表,然后制作一个站点的探测器列表。这是 XML 的一小部分。它会有多个 StationInventory 元素。

我将数据用作 asp:xml 控件的源,并将 xslt 文件用作转换源。

        var service = new InternalService();
        var result = service.StationInventory();

        invXml.DocumentContent = result;
        invXml.TransformSource = "StationInventory.xslt";
        invXml.DataBind();

当然感谢任何提示。周末愉快。

干杯, ~ck

【问题讨论】:

  • 好问题 (+1)。有关问题的解释和完整的解决方案,请参阅我的答案。

标签: c# asp.net xml xslt


【解决方案1】:

替换为

  <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:st="http://www.dummy-tmdd-address">
<xsl:template match="/">
  <div>
  <h2>Station Inventory</h2>
  <hr/>
  <xsl:apply-templates/>
  </div>
</xsl:template>

 <xsl:template match="st:StationInventory">
   <h5><xsl:value-of select="st:station-name" /></h5>
    <ul>
        <xsl:apply-templates select="st:detector-list/st:detector"/>
    </ul>
  </xsl:template>


  <xsl:template match="st:detector">
   <li>
     <xsl:value-of select="st:detector-name" />
  </li>
  </xsl:template>
 </xsl:stylesheet>

因为检测器是检测器列表的子代,而不是站点库存,并且存在命名空间

【讨论】:

  • 美丽。非常感谢!
【解决方案2】:

有两个明显的问题

  1. XML 文档中的所有元素都在默认命名空间中,但在 XSLT 代码中它们被引用为属于“无命名空间”。

  2. 元素&lt;StationInventory&gt; 没有任何&lt;detector&gt; 子元素。

解决方案

在下面的 XSLT 样式表中,上面的两个问题都得到了纠正:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:d="http://www.dummy-tmdd-address">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <div>
  <h2>Station Inventory</h2>
  <hr/>
  <xsl:apply-templates/>
  </div>
 </xsl:template>

 <xsl:template match="d:StationInventory">
   <h5><xsl:value-of select="d:station-name" /></h5>
   <xsl:apply-templates select="d:detector-list/d:detector"/>
 </xsl:template>

  <xsl:template match="d:detector">
   <span>
     <xsl:value-of select="d:detector-name" />
   </span>
   <br/>
  </xsl:template>
 </xsl:stylesheet>

现在的结果是完整的输出,很可能是想要的

<div xmlns:d="http://www.dummy-tmdd-address">
    <h2>Station Inventory</h2>
    <hr />
    <h5>Zone 9940-SEB</h5>
    <span>1999 West Smith Exit SEB</span>
    <br />
    <span>CR-155 Exit SEB</span>
    <br />
    <span>Pt of View SEB</span>
    <br />
</div>

【讨论】:

  • 漂亮。非常感谢您解释问题。你的答案也是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多