我正在尝试将xml文件转换为JSON。到目前为止我有这个
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" version="1.0" encoding="ISO-8859-1" indent="yes"/>
<xsl:template match="batch">
<xsl:for-each select="//batchitem/materialcode">
{
"load_id" : "<xsl:value-of select="//batch/code"/>"
"amount" : "<xsl:value-of select="//batch/amount"/>"
"weighlistfactor" : "<xsl:value-of select="//batch/weighlistfactor"/>"
"watercorrection" : "<xsl:value-of select="//batch/watercorrection"/>"
"materialcode" : "<xsl:value-of select="//batch/sequence"/>"
"code" : "<xsl:value-of select="//batch/deliverynote/code"/>"
"materialcode" : "<xsl:value-of select="//batch/batchitems/batchitem/materialcode"/>"
"materialname" : "<xsl:value-of select="//batch/batchitems/batchitem/materialname"/>"
}
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
这是XML文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<batch>
<code>213768</code>
<amount unit="m3">2.33</amount>
<weighlistfactor>2.33</weighlistfactor>
<watercorrection>-6</watercorrection>
<sequence>7</sequence>
<mixer>Menger</mixer>
<deliverynote>
<code>2130021903</code>
<id>97756</id>
<recipeinstance>NL9560</recipeinstance>
<recipecode>111270</recipecode>
<recipename>NCODE_1NHCPG57</recipename>
<site>NL4101</site>
<loadingpoint>NL41-01</loadingpoint>
<date>2016-02-12</date>
<plannedproductionstart>2016-02-12T12:15:04</plannedproductionstart>
<start_datetime>2016-02-12T12:15:04</start_datetime>
<end_datetime>2016-02-12T12:23:32</end_datetime>
<amount unit="m3">7</amount>
<manualbatched>No</manualbatched>
<order>2130021903</order>
<startloadmixer>2016-02-12T12:16:49</startloadmixer>
<truck>
<code>NLM9279</code>
</truck>
<product>
<code></code>
<name></name>
</product>
</deliverynote>
<batchitems>
<batchitem>
<materialcode>108954</materialcode>
<materialname>Rivierzand 0/4</materialname>
<materialkind>sand</materialkind>
<bin>
<externalid>Z2</externalid>
<hopper>28212</hopper>
<hoppername>Zandweger</hoppername>
</bin>
<target unit="kg">1920</target>
<targetadjusted unit="kg">1920</targetadjusted>
<amount unit="kg">1925</amount>
<moisture unit="%">4.4</moisture>
<density unit="kg/l">2.625</density>
<absorption unit="%">0.5</absorption>
</batchitem>
<batchitem>
<materialcode>109677</materialcode>
<materialname>Riviergrind 2/8</materialname>
<materialkind>stone</materialkind>
<bin>
<externalid>G3</externalid>
<hopper>28318</hopper>
<hoppername>Grindweger</hoppername>
</bin>
<target unit="kg">1890</target>
<targetadjusted unit="kg">1390</targetadjusted>
<amount unit="kg">1400</amount>
<moisture unit="%">4.4</moisture>
<density unit="kg/l">2.58</density>
<absorption unit="%">2</absorption>
</batchitem>
<batchitem>
<materialcode>60334949</materialcode>
<materialname>CEM III/B 42,5 N</materialname>
<materialkind>cement</materialkind>
<bin>
<externalid>C3a</externalid>
<hopper>28386</hopper>
<hoppername>Cementweger</hoppername>
</bin>
<target unit="kg">465</target>
<targetadjusted unit="kg">465</targetadjusted>
<amount unit="kg">460</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">2.95</density>
</batchitem>
<batchitem>
<materialcode>60334946</materialcode>
<materialname>CEM I 52,5 R</materialname>
<materialkind>cement</materialkind>
<bin>
<externalid>C4a</externalid>
<hopper>28386</hopper>
<hoppername>Cementweger</hoppername>
</bin>
<target unit="kg">465</target>
<targetadjusted unit="kg">465</targetadjusted>
<amount unit="kg">465</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">3.15</density>
</batchitem>
<batchitem>
<materialcode>60335044</materialcode>
<materialname>Vliegas (met K-factor)</materialname>
<materialkind>filler</materialkind>
<bin>
<externalid>C2</externalid>
<hopper>28386</hopper>
<hoppername>Cementweger</hoppername>
</bin>
<target unit="kg">420</target>
<targetadjusted unit="kg">420</targetadjusted>
<amount unit="kg">420</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">2.25</density>
</batchitem>
<batchitem>
<materialcode>60335001</materialcode>
<materialname>Cugla MMV con. 25%</materialname>
<materialkind>admixture</materialkind>
<bin>
<externalid>H2</externalid>
<hopper>28760</hopper>
<hoppername>Hulpstofweger 1</hoppername>
</bin>
<target unit="kg">0.93</target>
<targetadjusted unit="kg">0.93</targetadjusted>
<amount unit="kg">0.93</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">1.143</density>
</batchitem>
<batchitem>
<materialcode>60573193</materialcode>
<materialname>Cugla LR-9400 con 30% SPL</materialname>
<materialkind>admixture</materialkind>
<bin>
<externalid>H4</externalid>
<hopper>28760</hopper>
<hoppername>Hulpstofweger 1</hoppername>
</bin>
<target unit="kg">9.97</target>
<targetadjusted unit="kg">9.97</targetadjusted>
<amount unit="kg">9.97</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">1.053</density>
</batchitem>
<batchitem>
<materialcode>60719054</materialcode>
<materialname>Bronwater</materialname>
<materialkind>water</materialkind>
<bin>
<externalid>W1</externalid>
<hopper>28603</hopper>
<hoppername>Waterweger</hoppername>
</bin>
<target unit="kg">280</target>
<targetadjusted unit="kg">170</targetadjusted>
<amount unit="kg">164</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">1</density>
<temperature unit="°C">1000</temperature>
</batchitem>
<batchitem>
<materialcode>109678</materialcode>
<materialname>Riviergrind 4/16</materialname>
<materialkind>stone</materialkind>
<bin>
<externalid>G1</externalid>
<hopper>28318</hopper>
<hoppername>Grindweger</hoppername>
</bin>
<target unit="kg">0</target>
<targetadjusted unit="kg">500</targetadjusted>
<amount unit="kg">500</amount>
<moisture unit="%">3.5</moisture>
<density unit="kg/l">2.62</density>
<absorption unit="%">1.8</absorption>
</batchitem>
<batchitem>
<materialcode>60719054</materialcode>
<materialname>Bronwater</materialname>
<materialkind>water</materialkind>
<bin>
<externalid>W1</externalid>
<hopper>28603</hopper>
<hoppername>Waterweger</hoppername>
</bin>
<target unit="kg">0</target>
<targetadjusted unit="kg">100</targetadjusted>
<amount unit="kg">96</amount>
<moisture unit="%">0</moisture>
<density unit="kg/l">1</density>
<temperature unit="°C">1000</temperature>
</batchitem>
</batchitems>
</batch>
我的问题是它为每个循环打印出相同的东西。我只打印出它遇到的第一件事并重复这样做。 Here是我的目标的一个例子,我无法区别对待。
答案 0 :(得分:1)
我只打印出它遇到的第一件事并做到了 反复。
这样做是因为你反复选择同一件事。
以//
开头的XPath表达式从根开始选择整个文档中的所有引用节点。并且(在XSLT 1.0中)xsl:value-of
返回所选集合中第一个节点的值。
您没有发布预期结果,因此我只能猜测您想要执行以下操作:
<xsl:template match="batch">
<xsl:for-each select="//batchitem">
{
"load_id" : "<xsl:value-of select="//batch/code"/>"
"amount" : "<xsl:value-of select="//batch/amount"/>"
"weighlistfactor" : "<xsl:value-of select="//batch/weighlistfactor"/>"
"watercorrection" : "<xsl:value-of select="//batch/watercorrection"/>"
"materialcode" : "<xsl:value-of select="//batch/sequence"/>"
"code" : "<xsl:value-of select="//batch/deliverynote/code"/>"
"materialcode" : "<xsl:value-of select="materialcode"/>"
"materialname" : "<xsl:value-of select="materialname"/>"
}
</xsl:for-each>
</xsl:template>
其中materialcode
和materialname
的值由相对 Xpath表达式检索,从当前batchitem
节点开始。