【问题标题】:Value-of in won't show i XML file when transformed转换后的值不会显示 i XML 文件
【发布时间】:2016-12-07 10:42:39
【问题描述】:

我有一个如下所示的 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>

但是当我在 VS 中的 XslCompiledTranform 方法中运行它时,我在 .json 结果文件中没有得到任何值。

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

这是我的 XML 文件:

<?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>

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

    { "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":""	}
  

【问题讨论】:

    标签: c# json xml xslt


    【解决方案1】:

    要选择您需要的属性,例如&lt;xsl:value-of select="@Id"/&gt; 而不是 &lt;xsl:value-of select="Id"/&gt;,因此您需要更改所有这些路径。

    【讨论】:

      猜你喜欢
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      相关资源
      最近更新 更多