2017-08-17 157 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

是的,謝謝邁克爾它應該對我有用。 –