Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js XSLT XML到HTML的两阶段转换-必须是更好的方法_Node.js_Xml_Xslt_Saxon Js - Fatal编程技术网

Node.js XSLT XML到HTML的两阶段转换-必须是更好的方法

Node.js XSLT XML到HTML的两阶段转换-必须是更好的方法,node.js,xml,xslt,saxon-js,Node.js,Xml,Xslt,Saxon Js,我们得到一个价格变化的XML数据包,然后想要更新HTML文档的特定部分。问题是,我们看到它工作的唯一方法是通过两个阶段的转换,首先将XML数据包转换为格式良好的HTML块,然后第二个XSLT读取HTML文件并覆盖该特定部分 要更新的HTML文件(格式正确): 我们发现这是不正确的(或者是做错了)。如果我们只是将XSL传递给转换函数(stylesheetFileName:),就会产生一个错误。我认为您基本上想要一个沿着 <?xml version="1.0" encodi

我们得到一个价格变化的XML数据包,然后想要更新HTML文档的特定部分。问题是,我们看到它工作的唯一方法是通过两个阶段的转换,首先将XML数据包转换为格式良好的HTML块,然后第二个XSLT读取HTML文件并覆盖该特定部分

要更新的HTML文件(格式正确):


我们发现这是不正确的(或者是做错了)。如果我们只是将XSL传递给转换函数(stylesheetFileName:),就会产生一个错误。

我认为您基本上想要一个沿着

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:param name="price-data">
<supplier>
  <product>
    <key>3</key>
    <pprice uptype="1">
      <price>$22.34</price>
    </pprice>
  </product>
</supplier>
  </xsl:param>
  
  <xsl:key name="price" match="product/pprice[@uptype = 1]/price" use="'price' || ancestor::product/key"/>

  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:template match="td[@name][key('price', @name, $price-data)]/text()">{key('price', ../@name, $price-data)}</xsl:template>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
  </xsl:template>
  
</xsl:stylesheet>

fn:transform
指的是XPath 3.1函数,而不是Saxon API方法。您能澄清一下您是在浏览器中还是在Node.JS下运行Saxon JS吗?您在哪里读到不建议使用参数?对我来说,这听起来是个很糟糕的建议。编写没有参数的样式表是双手绑在背后的工作。@MichaelKay在node.js下使用它。我需要买你的书。Martin下面的解决方案说明了我们缺乏XSL技能。或者,如果更方便的话,您可以使用document()函数按需加载“辅助数据”。@MichaelKay,对,是的,我认为Saxon JS中的异步处理需要预加载,因此没有提到
doc
doc
函数,但我编辑了答案,提到了更简单的方法,并测试了它与Saxon JS 2.1和一个简单的,相对的基于文件的加载没有任何问题。当然,如果您希望一切都能顺利地异步运行,预加载会更好。@MartinHonnen感谢您提供的示例代码。但是它没有xsl来加载HTML文件(/home/tireduser/node/bigship/public/updatehtml.HTML)。我要使用我现有的代码吗?@ImTalkingCode,据我所知,您可以将格式良好的HTML文件作为上述样式表的主要输入。
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="xml-price.xsl"?>
<supplier>
  <product>
    <key>3</key>
    <pprice uptype="1">
      <price>$22.34</price>
    </pprice>
  </product>
</supplier>
<xsl:stylesheet ...>
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:template match="/supplier">
    <xsl:apply-templates select="product"/>
  </xsl:template>
  <xsl:template match="product">
    <xsl:variable name="PKey">
      <xsl:value-of select="key"/>
    </xsl:variable>
    <xsl:for-each select="pprice">  <!-- could be more than 1 -->
      <xsl:choose>
        <xsl:when test="@uptype=0">
        </xsl:when>
        <xsl:when test="@uptype=1">
          <xsl:apply-templates select="price"/>
        </xsl:when>
        <xsl:otherwise>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="price">
      <td name="rate$PKey"><xsl:value-of select="."/></td>
  </xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="td[@name='price3']">   <!-- Problem 1 -->
    <td name="price3">$22.34</td>             <!-- Problem 2 --> 
  </xsl:template>
  <xsl:template match="/">
    <xsl:apply-templates select="document('/home/tireduser/node/bigstuff/public/update-html.html')/node()"/>
  </xsl:template>
</xsl:stylesheet>
Using fn:transform()
If a source XSLT stylesheet is supplied as input to the fn:transform() function in XPath, the XX compiler will be invoked to compile the stylesheet before it is executed. However, there is no way of capturing the intermediate SEF stylesheet for subsequent re-use.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:param name="price-data">
<supplier>
  <product>
    <key>3</key>
    <pprice uptype="1">
      <price>$22.34</price>
    </pprice>
  </product>
</supplier>
  </xsl:param>
  
  <xsl:key name="price" match="product/pprice[@uptype = 1]/price" use="'price' || ancestor::product/key"/>

  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:template match="td[@name][key('price', @name, $price-data)]/text()">{key('price', ../@name, $price-data)}</xsl:template>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
  </xsl:template>
  
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  expand-text="yes">
  
  <xsl:param name="price-data" as="xs:string"><![CDATA[
<supplier>
  <product>
    <key>3</key>
    <pprice uptype="1">
      <price>$22.34</price>
    </pprice>
  </product>
</supplier>
  ]]></xsl:param>
  
  <xsl:param name="price-doc" select="parse-xml($price-data)"/>
  
  <xsl:key name="price" match="product/pprice[@uptype = 1]/price" use="'price' || ancestor::product/key"/>

  <xsl:mode on-no-match="shallow-copy"/>
  
  <xsl:template match="td[@name][key('price', @name, $price-doc)]/text()">{key('price', ../@name, $price-doc)}</xsl:template>

  <xsl:template match="/" name="xsl:initial-template">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
  </xsl:template>
  
</xsl:stylesheet>