为什么"xsl:value-of"(不再)工作,我该如何解决?



我有一个在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(为了缩短它,我删除了一些,包括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。

谢谢你的帮助!

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

最新更新