【问题标题】:XSLT Newbie and XML ArrayXSLT 新手和 XML 数组
【发布时间】:2010-11-21 06:42:19
【问题描述】:

我有一些非常基本的 XML:

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>One</string>
<string>Two</string>
</ArrayOfString>

我怎样才能把它翻译成:

<ul>
 <li>One</li>
 <li>Two</li>
</ul>

使用 XSLT?

以前我使用过 a:value 但这些只是字符串?

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    我愿意:

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
    >
      <xsl:template match="ArrayOfString">
        <ul>
          <xsl:apply-templates select="string" />
        </ul>
      </xsl:template>
    
      <xsl:template match="string">
        <li>
          <xsl:value-of select="." />
        </li>
      </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      你想要这样的东西吗?

      <?xml version='1.0'?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
          <ul>
          <xsl:apply-templates />
          </ul>
      </xsl:template>
      <xsl:template match="string">
          <li><xsl:value-of select="text()"></xsl:value-of></li>
      </xsl:template>
      </xsl:stylesheet>
      

      它为我产生以下输出:

      <?xml version='1.0' ?>
      <ul>
      <li>One</li>
      <li>Two</li>
      </ul>
      

      【讨论】:

      • 您的“/”模板只能“偶然”工作。您应该使模板显式而不是信任隐式默认规则来做正确的事情。一旦在样式表的其他地方定义了ArrayOfString 的实际模板,这个解决方案就会中断。
      • 很公平。我刚开始使用我的编辑器提供的基本 XSL 模板并添加了一些东西,直到它起作用:)
      • 我有 Stylus Studio 2007 - 这些天对 XML 做的不多,所以它没有升级:stylusstudio.com/xml_product_index.html
      猜你喜欢
      • 1970-01-01
      • 2010-12-11
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-30
      • 2010-11-10
      • 1970-01-01
      相关资源
      最近更新 更多