在转换时,不会显示i XML文件的值

时间:2016-12-07 10:42:40

标签: c# json xml xslt

我有一个如下所示的XSL文件:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.loc.gov/mods/v3">

  <xsl:output method="text" encoding="UTF-8" media-type="text/plain"/>

  <xsl:template match="/">
    <!-- Fetch all products -->
    <xsl:apply-templates select="Data/Products" />
  </xsl:template>

  <xsl:template match="Products">
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"<xsl:value-of select="Id"/>",
	"name":"<xsl:value-of select="name"/>"
    "description":"<xsl:value-of select="description"/>",
    "Category 1":"<xsl:value-of select="cat1"/>",
	"Category 2":"<xsl:value-of select="cat2"/>",
	"Category 3":"<xsl:value-of select="cat3"/>",
	"Category 4":"<xsl:value-of select="cat4"/>"	}
  </xsl:template>

</xsl:stylesheet>
&#13;
&#13;
&#13;

但是当我在VS中的XslCompiledTranform-method中运行它时,我不会在.json结果文件中获取任何值。

&#13;
&#13;
var myXslTrans = new XslCompiledTransform();
            myXslTrans.Load("Products.xsl");
            myXslTrans.Transform("Products.xml", "result_data_products.json");
&#13;
&#13;
&#13;

这是我的XML文件:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<Data>
  <Products Id="1" name="Golf1" description="En langsom golf bil" cat1="Bil" cat2="VW" cat3="Golf" cat4="1" />
  <Products Id="2" name="Golf2" description="En knap så langsom golf bil. Faktisk er den næsten hurtig." cat1="Bil" cat2="VW" cat3="Golf" cat4="2" />
  <Products Id="3" name="Golf3" description="Det er ikke en golf. Det er et billede af en golf. Bil medfølger." cat1="Bil" cat2="VW" cat3="Golf" cat4="3" />
  <Products Id="4" name="Golf4" description="Den aller hurtigste bil. Hurtigere end en langsommere." cat1="Bil" cat2="VW" cat3="Golf" cat4="4" />
  <Products Id="5" name="7 jern" description="God kølle til tæsk og golf." cat1="Sport" cat2="Golf" cat3="Køller" cat4="" />
  <Products Id="6" name="19 jern" description="God til golf. Dårlig til tæsk." cat1="Sport" cat2="Golf" cat3="Køller" cat4="" />
  <Products Id="7" name="Golfbold" description="Rund golfbold" cat1="Sport" cat2="Golf" cat3="Bolde" cat4="Hvid" />
  <Products Id="8" name="Golfbold" description="Noget man slår til med sin kølle." cat1="Sport" cat2="Golf" cat3="Bolde" cat4="Sort" />
</Data>
&#13;
&#13;
&#13;

这是.json格式的结果,其中没有插入任何值。

&#13;
&#13;
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
    { "index":{"_index":"test-product", "_type":"Product"}}
    { "Id":"",
	"name":""
    "description":"",
    "Category 1":"",
	"Category 2":"",
	"Category 3":"",
	"Category 4":""	}
  
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

选择您需要的属性,例如<xsl:value-of select="@Id"/>而非<xsl:value-of select="Id"/>因此您需要更改所有这些路径。