【问题标题】:why is "xsl:value-of" not working (anymore) and how can I resolve it?为什么“xsl:value-of”(不再)不起作用,我该如何解决?
【发布时间】:2019-05-26 04:08:56
【问题描述】:

我有一个在 Windows (.NET Framework) 上运行的爬虫服务来爬取不同的提要并创建新的 xml 提要。它是大约 10 年前建造的,并且有一段时间没有使用(大约 2 年)。如果我现在尝试在 Windows Server 2012 R2 上运行它,一切顺利,除了检索“xsl:value-of”的值。我正在尝试解决这个问题(过去曾经运行过)。谁能告诉我我错过了什么?

如果我使用静态文本,它会起作用。如果我将 xsl:call-template 与 xsl:with-param 一起使用(请参见下面的代码),也会检索信息。只有 xsl:value-of 不检索值。

这是 XML 的一部分:

<?xml version="1.0" encoding="utf-8"?>
<products>
    <product>
        <productID>175436</productID>
        <name>Best Stay Hotel</name>
        <description><![CDATA[A nice place.]]></description>
        <additional>
            <field name="country">Cyprus</field>
        </additional>
    </product>
</products>

这是我的 XSLT(我已经删除了一些以使其更短,include.xslt 按预期工作):

<?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:output method="xml" indent="yes"/>

  <xsl:template match="/products">
    <xml>
      <products>
        <xsl:apply-templates select="product"/>
      </products>
    </xml>
  </xsl:template>
  <xsl:template match="product">
    <product>
      <xsl:attribute name="index">
        <xsl:value-of select="position()"/>
      </xsl:attribute>
      <pprSubsiteID>
        <xsl:value-of select="normalize-space(productID)" />
      </pprSubsiteID>
      <details>
        <pprName>
         <xsl:call-template name="removeHtmlTags">
                <xsl:with-param name="html" select="normalize-space(name)" />
            </xsl:call-template>
        </pprName>
        <pprCountry>
          <xsl:value-of select="normalize-space(additional/field[@name='country'])"/>
        </pprCountry>
        <pprDescription>
         <xsl:call-template name="removeHtmlTags">
           <xsl:with-param name="html" select="normalize-space(description)" />
         </xsl:call-template>
        </pprDescription>
      </details>
    </product>
  </xsl:template>
  <xsl:template name="removeHtmlTags">
    <xsl:param name="html"/>
    <xsl:choose>
        <xsl:when test="contains($html, '&lt;')">
            <xsl:value-of select="substring-before($html, '&lt;')"/>
            <!-- Recurse through HTML -->
            <xsl:call-template name="removeHtmlTags">
                <xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$html"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

这是输出的样子:

  <?xml version="1.0" encoding="UTF-8"?>
  <xml>
     <products>
        <product index="1">
           <pprSubsiteID>175436</pprSubsiteID>
           <details>
              <pprName>Best Stay Hotel</pprName>
              <pprCountry>Cyprus</pprCountry>
              <pprDescription>A nice place.</pprDescription>
           </details>
        </product>
     </products>
  </xml>

这是我得到的结果:

  <?xml version="1.0" encoding="UTF-8"?>
  <xml>
     <products>
        <product index="1">
           <pprSubsiteID></pprSubsiteID>
           <details>
              <pprName>Best Stay Hotel</pprName>
              <pprCountry></pprCountry>
              <pprDescription>A nice place.</pprDescription>
           </details>
        </product>
     </products>
  </xml>

所以 pprSubsiteID 和 pprCountry 丢失了。

感谢您的帮助!

【问题讨论】:

  • 您能否确认您所展示的实际上是正在使用的内容,如果是这种情况,它应该可以按预期工作? (见xsltfiddle.liberty-development.net/3NzcBuk)。实际上是否有任何元素上的 xmlns="..." 形式的命名空间声明?谢谢
  • 我知道...它以前可以工作,但我不明白为什么现在不能工作。如果我自己使用工具在线测试它并且它确实有效。可能与服务器有关吗?我认为它之前是 Windows 2008 Server。还是 .NET 框架?
  • 你能回答蒂姆提出的问题吗?
  • 不确定如何检查。我如何/在哪里可以做到这一点?
  • 您的输入 XML 中要么有 xmlns="...",要么没有。另外,请修复 XSLT 示例:一方面,它没有正确嵌套,缺少关闭 &lt;/details&gt;。另一方面,它包含对您未显示的代码的引用。在您的问题中包含removeHtmlTags 模板的定义,或者从您的XSLT 中删除对它的任何引用。代码示例应该是完全独立的,因此人们可以实际复制和运行它们。

标签: xml xslt xslt-1.0


【解决方案1】:
   <xsl:template match="products">
    <xml>
        <products>
            <product>
                <xsl:attribute name="index">
                    <xsl:value-of select="position()"/>
                </xsl:attribute>
                <pprSubsiteID><xsl:value-of select="//productID"/></pprSubsiteID>
                <details>
                    <pprName><xsl:value-of select="//name"/></pprName>
                    <pprCountry><xsl:value-of select="//additional/field"/></pprCountry>
                    <pprDescription><xsl:value-of select="//description"/></pprDescription>
                </details>
            </product>
        </products>
    </xml>
</xsl:template>

【讨论】:

  • 谢谢。我试过了,但不幸的是这并没有给我想要的输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 2013-03-05
  • 1970-01-01
  • 2023-02-02
  • 2019-12-26
  • 2020-12-27
相关资源
最近更新 更多