2017-08-17 185 views
-1

我正尝试使用xml-to-json函数将XML数据转换为XSLT 3.0中的JSON。 任何人都可以为我提供Xslt 3.0以满足以下要求。XSLT 3.0中的XML到JSON转换

示例XML是:

<widget> 
<debug>on</debug> 
<window title="Sample Konfabulator Widget"> 
    <name>main_window</name> 
    <width>500</width> 
    <height>500</height> 
</window> 
<image src="Images/Sun.png" name="sun1"> 
    <hOffset>250</hOffset> 
    <vOffset>250</vOffset> 
    <alignment>center</alignment> 
</image> 
<text data="Click Here" size="36" style="bold"> 
    <name>text1</name> 
    <hOffset>250</hOffset> 
    <vOffset>100</vOffset> 
    <alignment>center</alignment> 
    <onMouseUp> 
     sun1.opacity = (sun1.opacity/100) * 90; 
    </onMouseUp> 
</text> 

我预期的JSON输出是:

{"widget": { 
"debug": "on", 
"window": { 
    "title": "Sample Konfabulator Widget", 
    "name": "main_window", 
    "width": 500, 
    "height": 500 
}, 
"image": { 
    "src": "Images/Sun.png", 
    "name": "sun1", 
    "hOffset": 250, 
    "vOffset": 250, 
    "alignment": "center" 
}, 
"text": { 
    "data": "Click Here", 
    "size": 36, 
    "style": "bold", 
    "name": "text1", 
    "hOffset": 250, 
    "vOffset": 100, 
    "alignment": "center", 
    "onMouseUp": "sun1.opacity = (sun1.opacity/100) * 90;" 
} 

}}

由于提前

+0

你有什么尝试?这不是一个免费的代码写入服务。 –

回答

1

您需要改造你的XML到XML词汇表由XML到JSON的功能预期,这可以用做模板的规则,如

<xsl:template match="*[*|@*]" mode="to-json"> 
    <fn:map key="{local-name()}"> 
    <xsl:apply-templates select="@*, *" mode="to-json"/> 
    </fn:map> 
</xsl:template> 

<xsl:template match="@* | *[not(*)]" mode="to-json"> 
    <fn:string key="{local-name()}"> 
    <xsl:value-of select="."/> 
    </fn:string> 
</xsl:template> 

,然后通过这个转变的XML到JSON函数的结果。

细节将取决于你想要做的关于命名空间,你想怎么检测元素/属性应被视为数字或布尔值,是否要产生null或“”空元素等

什么
+0

是的,谢谢迈克尔它应该对我有用。 –