通过XSL文件查询数据库

本文关键字:数据库 查询 文件 XSL 通过 | 更新日期: 2023-09-27 18:35:42

我有一个电子商务购物车,它在运行时生成XML。我可以访问设置实际 XML 样式的 XSL 样式表。

我想在菜单中显示产品类别图片。是否有您可以在 xsl 文件中运行查询的示例,您可以向我展示?

我看不出它从哪里提取类别数据?

你能帮忙吗?

<?xml version="1.0" standalone="yes" ?>
    <!-- ###################################################################################################### -->
    <!-- Copyright AspDotNetStorefront.com, 1995-2009. All Rights Reserved. -->
    <!-- http://www.aspdotnetstorefront.com -->
    <!-- For details on this license please visit the product homepage at the URL above. -->
    <!-- THE ABOVE NOTICE MUST REMAIN INTACT. -->
    <!-- ###################################################################################################### -->
    <package version="2.1" displayname="Categories" debug="false" includeentityhelper="true">
    <PackageTransform>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aspdnsf="urn:aspdnsf" exclude-result-prefixes="aspdnsf">
    <xsl:output method="html" omit-xml-declaration="yes"/>
    <xsl:param name="CategoryID">
    <xsl:choose>
    <xsl:when test="/root/System/PageName = 'showmanufacturer.aspx' or /root/System/PageName = 'showsection.aspx' or /root/System/PageName = 'showdistributor.aspx' or /root/System/PageName = 'showvector.aspx' or /root/System/PageName = 'showgenre.aspx'">0</xsl:when>
    <xsl:when test="/root/System/PageName = 'showcategory.aspx' and boolean(/root/QueryString/categoryid)">
    <xsl:value-of select="/root/QueryString/categoryid"/>
    </xsl:when>
    <xsl:when test="(/root/System/PageName = 'showcategory.aspx' or /root/System/PageName = 'showproduct.aspx') and boolean(/root/Cookies/LastViewedEntityInstanceID) and /root/Cookies/LastViewedEntityName = 'Category'">
    <xsl:value-of select="/root/Cookies/LastViewedEntityInstanceID"/>
    </xsl:when>
    <xsl:otherwise>
    0
    </xsl:otherwise>
    </xsl:choose>
    </xsl:param>
    <xsl:param name="AncestorID">
    <xsl:for-each select="/root/EntityHelpers/Category//Entity[EntityID = $CategoryID]">
    <xsl:value-of select="ancestor::*/EntityID"/>
    </xsl:for-each>
    </xsl:param>
    <xsl:param name="ParentID">
    <xsl:for-each select="/root/EntityHelpers/Category//Entity[EntityID = $CategoryID]">
    <xsl:value-of select="parent::*/EntityID"/>
    </xsl:for-each>
    </xsl:param>

    <xsl:template match="/">
    <xsl:element name="ul">
    <xsl:attribute name="class">
    <![CDATA[menuul]]>
    </xsl:attribute>
    <xsl:apply-templates select="/root/EntityHelpers/Category/Entity">
    <xsl:with-param name="prefix" select="''"/>
    </xsl:apply-templates>
    </xsl:element>
    </xsl:template>
    <xsl:template match="Entity">
    <xsl:param name="prefix"></xsl:param>
    <xsl:param name="eName" select="aspdnsf:GetMLValue(Name)" />
    <xsl:choose>
    <xsl:when test="Published=1">
    <li class="menuli">
    <xsl:value-of select="$prefix" />
    <!--<xsl:if test="number(ParentEntityID) != 0">
    <span class="catMark">>></span>�
    </xsl:if>-->
    <a href="{concat('c-',EntityID,'-',SEName,'.aspx')}">
    <xsl:if test="EntityID = $CategoryID or descendant::Entity/EntityID = $CategoryID">
    <xsl:attribute name="class">MenuSelected</xsl:attribute>
    </xsl:if>
    <xsl:value-of select="$eName" disable-output-escaping="yes"/>
    </a>

    <xsl:if test="count(child::Entity)>0">
    <ul class="submenuul">
    <xsl:apply-templates select="Entity">
    <xsl:with-param name="prefix" select="concat($prefix, '��')"/>
    </xsl:apply-templates>
    </ul>
    </xsl:if>
    </li>
    </xsl:when>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>
    </PackageTransform>
    </package>

通过XSL文件查询数据库

如果您使用的是 ASP.NET 则可以使用标准 ASP.NET XML Control

在页面加载时,将您的 xml 和 xsl 文件分配给它,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
   Xml1.DocumentSource = "~/App_Data/YourXmlFile.xml";
   Xml1.TransformSource = "~/App_Data/YourXslStyleSheetFile.xsl";
}

您还可以使用xml不存储在文件中而是存储在数据库中DocumentContent属性。

在此处查看示例

接受的答案对 ASP.Net 是正确的,但对Aspdotnetstorefront则不然。问题中引用的 XML 文件仅在启用调试时生成,并且不应在任何生产环境中生成(最终会遇到写入冲突)。

XSLT 背后的 XML 是在运行时为您生成的。包括类别数据(和其他实体数据)是因为您有includeentityhelper="true" .实体帮助程序缓存类别(和其他实体)数据以减少数据库查询。如果您还需要除此之外的数据,则应使用此处记录的 XMLPackage <query> 节点(在"SQL 查询"下):http://manual.aspdotnetstorefront.com/p-157-xml-packages.aspx

您还将找到默认构建中包含的大多数 XMLPackage 中使用的 '' 节点。