XSL使用MSXML将答案组合在一起

时间:2014-03-11 06:35:29

标签: javascript xml xslt

G'Day全部, 由于我无法使用key()来处理我的设置,所以有点困在这一点上。

好的,这些笨重,所以忍受我.. 发生的事情是我们从一个程序运行,一旦触发报告,它将运行一个调用* .js的HTML文件,将XML和XSL加载到浏览器中(通常是Firefox或IE - 因为ActiveX控件)

示例HTML:

<html>
  <head>
     <script language="JavaScript" src="Common/Functions.js"></script>
  </head>
  <body onload="displayResult('Common/Hardware.xsl');">
     <div id="report"></div>
  </body>
</html>

示例JS

function loadXMLDoc(dname)
{
  if (window.ActiveXObject)
  {
    xmldom = new ActiveXObject("Microsoft.XMLDOM");
    xmldom.async = false;
    xmldom.load(dname);
    return xmldom;
  }
  else
  {
    xhttp=new XMLHttpRequest();
    xhttp.open("GET",dname,false);
    xhttp.send("");
    return xhttp.responseXML;
  }
}

function displayResult(stylename)
{
  xml=loadXMLDoc("TempReportData/ExportData.xml");
  xsl=loadXMLDoc(stylename);

  if (window.ActiveXObject)
  {
    document.getElementById("report").innerHTML=xml.transformNode(xsl);
  }
  else
  {
    xsltProcessor=new XSLTProcessor();
    xsltProcessor.importStylesheet(xsl);
    resultDocument = xsltProcessor.transformToFragment(xml,document);
    if (document.getElementById("report").hasChildNodes())
    {
      document.getElementById("report").removeChild(document.getElementById("report").lastChild);
    }
    document.getElementById("report").appendChild(resultDocument);
  }
}

示例XML:

<?xml version="1.0"?>
<Report schema="1.0">
    <Item id="1" name="cabinet">
        <Properties></Properties>
        <VSection>
            <HSection id="60">
                <Component id="1" idfull="00303101">
                    <DisplayName>HardwareHandle</DisplayName>
                    <Category>Hardware</Category>
                    <Brand>CabinetWare</Brand>
                    <Sell>$0.00</Sell>
                    <Quantity>1</Quantity>
                </Component>
            </HSection>
            </VSetion>
        <VSection>
            <HSection id="62">
            </HSection>
        </VSection>
        <VSection>
            <HSection id="205">
                <Component id="1" idfull="003020501">
                    <DisplayName>comment</DisplayName>
                    <Category></Category>
                    <Brand></Brand>
                    <Sell>$0.00</Sell>
                    <Quantity>1</Quantity>
                </Component>
            </HSection>
        </VSection>
    </Item>
</Report>

示例XSL :(包含在评论请求中)

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes" />

<xsl:key name="Some_Brand" match="Component" select="Brand"/>
<!-- note the moment i ask for this Key the output is blank, if its commented out it below works fine-->

<xsl:template match="/">

<h1>Stock Order Form</h1>

<table>
<tr>
<th>Qty</th>
<th>Part No.</th>
<th>Description</th>
<th>Unit Cost</th>
</tr>
<xsl:apply-templates select="Report/Item"/>
</table>
</xsl:template>

<xsl:template match="Item">
<xsl:apply-templates select="VSection/HSection/Component"/>     
</xsl:template>

<!-- wanting to be able to group items in here, so instead of seeing "1x bolt" and another "1x bolt", i want to see "2x bolt"-->
<xsl:template match="Component">
<xsl:if test="Brand = 'Some_name'">
    <tr>
        <td><xsl:value-of select="Quantity"/></td>
        <td><xsl:value-of select="PartNo"/></td>
        <td><xsl:value-of select="Style"/></td>
        <td><xsl:value-of select="UnitCost"/></td>
    </tr>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

请记住,上面的XML非常简单..但我需要的是能够检查发生的每个项目,以及其中的每个组件,因为它们可以有几个但都在同一级别,报告/项目/ VSection / HSection / Component,我需要评估brand =“Some_name”并返回包含的一些答案,即我可能只想要该项目中与我的Brand i匹配的那个Component的数量或类别for,并忽略同一项中没有的其他组件。

我的一个问题是,当我尝试使用key()时,XSLT刚停止,输出为空。拿走它再次工作,我有一种感觉我的问题的开始是使用* .js文件,或使用MSXML,因为我在Muenchian方法下的某处读取它可能不会做键()???

无论如何,任何建议或帮助,或替代方法都会很棒。 (对不起,如果我的问题含糊不清,请告诉我,我可以编辑它,这是漫长的一天)

阿利斯泰尔。

1 个答案:

答案 0 :(得分:1)

正如评论中“Nick G”所述,“select”不是 xsl:key 语句中的有效属性,它应该是使用

<xsl:key name="Some_Brand" match="Component" use="Brand"/>

这假设您将按品牌值对组件元素进行分组。

就Muenchian分组而言,首先选择分量元素,这些元素首先出现在品牌的给定值的键中。

<xsl:apply-templates select="VSection/HSection/Component
                             [generate-id() = generate-id(key('Some_Brand', Brand)[1])]"/>

然后,在匹配组件的模板中,您可以访问组中的所有元素(即所有组件元素具有相同的品牌)使用密钥。例如,要对组中所有项目的总数量求和,请执行此操作

<xsl:value-of select="sum(key('Some_Brand', Brand)/Quantity)"/>

尝试将此XSLT作为入门者:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes" />

    <xsl:key name="Some_Brand" match="Component" use="Brand"/>

    <xsl:template match="/">
        <h1>Stock Order Form</h1>
        <table>
            <tr>
                <th>Qty</th>
                <th>Part No.</th>
                <th>Description</th>
            <th>Unit Cost</th>
            </tr>
            <xsl:apply-templates select="Report/Item"/>
        </table>
    </xsl:template>

    <xsl:template match="Item">
       <xsl:apply-templates select="VSection/HSection/Component[generate-id() = generate-id(key('Some_Brand', Brand)[1])]"/>
    </xsl:template>

    <xsl:template match="Component">
        <tr>
            <td><xsl:value-of select="Brand"/></td>
            <td><xsl:value-of select="sum(key('Some_Brand', Brand)/Quantity)"/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>