我对XSL很新。我正在尝试将XML转换为JSON
我正在使用的XSLT是`<xsl:template match="/node()">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="." mode="detect" />
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="*" mode="detect">
<xsl:choose>
<xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())">
<xsl:apply-templates select="." mode="obj-content" />
<xsl:text>]</xsl:text>
<xsl:if test="count(following-sibling::*[name() != name(current())]) > 0">, </xsl:if>
</xsl:when>
<xsl:when test="name(preceding-sibling::*[1]) = name(current())">
<xsl:apply-templates select="." mode="obj-content" />
<xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if>
</xsl:when>
<xsl:when test="following-sibling::*[1][name() = name(current())]">
<xsl:text>"</xsl:text><xsl:value-of select="name()"/><xsl:text>" : [</xsl:text>
<xsl:apply-templates select="." mode="obj-content" /><xsl:text>, </xsl:text>
</xsl:when>
<xsl:when test="count(./child::*) > 0 or count(@*) > 0">
<xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : <xsl:apply-templates select="." mode="obj-content" />
<xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
</xsl:when>
<xsl:when test="count(./child::*) = 0">
<xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/><xsl:text>"</xsl:text>
<xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="obj-content">
<xsl:text>{</xsl:text>
<xsl:apply-templates select="@*" mode="attr" />
<xsl:if test="count(@*) > 0 and (count(child::*) > 0 or text())">, </xsl:if>
<xsl:apply-templates select="./*" mode="detect" />
<xsl:if test="count(child::*) = 0 and text() and not(@*)">
<xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="text()"/><xsl:text>"</xsl:text>
</xsl:if>
<xsl:if test="count(child::*) = 0 and text() and @*">
<xsl:text>"text" : "</xsl:text><xsl:value-of select="text()"/><xsl:text>"</xsl:text>
</xsl:if>
<xsl:text>}</xsl:text>
<xsl:if test="position() < last()">, </xsl:if>
</xsl:template>
<xsl:template match="@*" mode="attr">
<xsl:text>"</xsl:text><xsl:value-of select="name()"/>" : "<xsl:value-of select="."/><xsl:text>"</xsl:text>
<xsl:if test="position() < last()">,</xsl:if>
</xsl:template>
<xsl:template match="node/@TEXT | text()" name="removeBreaks">
<xsl:param name="pText" select="normalize-space(.)"/>
<xsl:choose>
<xsl:when test="not(contains($pText, '
'))"><xsl:copy-of select="$pText"/></xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat(substring-before($pText, '
'), ' ')"/>
<xsl:call-template name="removeBreaks">
<xsl:with-param name="pText" select="substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
`
我需要输出
"InvoiceItems": [
{
"InvoiceLineNumber": "1",
"DivisionCode": "300",
"InvoicedQuantity": "1",
"ProductInvoicedPrice": "26.50",
"ProductName": "SAND DOLLARS S/S SHIRT",
"ProductNumber": "CSHAH7BLKM",
"InvoiceItemSizes": [
{
"InvoiceNumber": "101000003",
"InvoiceLineNumber": "1",
"SizeCode": "S",
"UPC": "",
"InvoicedQuantity": "1"
}
]
}
但我得到了输出
"InvoiceItems": [
{
"InvoiceItemSizes": {
"InvoiceLineNumber": "1",
"InvoicedQuantity": "3",
"SizeCode": "L",
"UPC": "9348282095644"
},
"InvoiceLineNumber": "1",
"InvoiceNumber": "101000005",
"InvoicedQuantity": "3",
"ProductInvoicedPrice": "39.00",
"ProductName": "MF TAKE TWO JACKET",
"ProductNumber": "CJKBC7BLKL"
},
{
"InvoiceItemSizes": {
"InvoiceLineNumber": "2",
"InvoicedQuantity": "3",
"SizeCode": "M",
"UPC": "9348282095637"
},
"InvoiceLineNumber": "2",
"InvoiceNumber": "101000005",
"InvoicedQuantity": "3",
"ProductInvoicedPrice": "39.00",
"ProductName": "MF TAKE TWO JACKET",
"ProductNumber": "CJKBC7BLKM"
}
这里的区别是在输出中我期待InvloiceItemSize值进入[]但是在输出中我得到它来自{}。请帮帮我。
如果有人有正确的XSLT从XML转换为JSON,请提供给我。
由于
答案 0 :(得分:-2)
让我为你做谷歌:
https://github.com/doekman/xml2json-xslt
http://www.bramstein.com/projects/xsltjson/
http://www.bjelic.net/2012/08/01/coding/convert-xml-to-json-using-xslt/
Stackoverflow上的How to convert json to xml using xslt
(只是搜索&#34; xslt xml 2 json&#34;的四个第一个链接,有一些非常好的解释)