2012-10-15 62 views
0

我輸入XML會是這樣轉換的JSONx陣列爲XML XSLT

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <json:object name="Login"> 
     <json:array name="Group"> 
      <json:object> 
       <json:string name="Name">john</json:string> 
       <json:string name="Password"/> 
      </json:object> 
      <json:object> 
       <json:string name="Name">john</json:string> 
       <json:string name="Password"/> 
      </json:object> 
     </json:array> 
    </json:object> 
</json:object> 
    <alice xmlns="http://some-namespace" xmlns:charlie="http://some-other-namespace"> 
    <bob>david</bob> 
    <charlie:edgar>frank</charlie:edgar> 
</alice> 
    { 
    "alice":{ 
     "bob":{ 
     "$":"david", 
     "@xmlns":{ 
      "charlie":"http:\/\/some-other-namespace", 
      "$":"http:\/\/some-namespace" 
     } 
     }, 
     "charlie:edgar":{ 
     "$":"frank", 
     "@xmlns":{ 
      "charlie":"http:\/\/some-other-namespace", 
      "$":"http:\/\/some-namespace" 
     } 
     }, 
     "@xmlns":{ 
     "charlie":"http:\/\/some-other-namespace", 
     "$":"http:\/\/some-namespace" 
     } 
    } 
} 

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.datapower.com/schemas/json jsonx.xsd"> 
    <json:object name="alice"> 
     <json:object name="bob"> 
      <json:string name="$">david</json:string> 
      <json:object name="@xmlns"> 
       <json:string name="charlie">http://some-other-namespace</json:string> 
       <json:string name="$">http://some-namespace</json:string> 
      </json:object> 
     </json:object> 
     <json:object name="charlie:edgar"> 
      <json:string name="$">frank</json:string> 
      <json:object name="@xmlns"> 
       <json:string name="charlie">http://some-other-namespace</json:string> 
       <json:string name="$">http://some-namespace</json:string> 
      </json:object> 
     </json:object> 
     <json:object name="@xmlns"> 
      <json:string name="charlie">http://some-other-namespace</json:string> 
      <json:string name="$">http://some-namespace</json:string> 
     </json:object> 
    </json:object> 
</json:object> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:json="http://www.ibm.com/xmlns/prod/2010/jsonx" xmlns:b="http://www.ibm.com/xmlns/prod/2009/jsonx" exclude-result-prefixes="json #default"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/json:object"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="json:array[@name]"> 
     <xsl:apply-templates mode="noname"> 
      <xsl:with-param name="nme" select="name()"/> 
     </xsl:apply-templates> 
    </xsl:template> 
    <xsl:template match="json:object" mode="noname"> 
     <xsl:param name="nme"/> 
     <xsl:element name="{../@name}" namespace="{namespace-uri()}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:array"> 
     <xsl:element name="{../@name}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:object[@name]"> 
     <xsl:if test="not(*[namespace-uri()=''])"> 
      <xsl:element name="{@name}" namespace="{namespace-uri()}"> 
       <!--    <xsl:copy-of select="namespace::*[name()]"/> 
       <xsl:apply-templates select="node()"/>--> 
       <xsl:apply-templates/> 
      </xsl:element> 
     </xsl:if> 
     <!--  <xsl:element name="{@name}"> 
      <xsl:apply-templates/> 
     </xsl:element>--> 
    </xsl:template> 
    <xsl:template match="json:string[@name]"> 
     <xsl:if test="not(*[namespace-uri()=''])"> 
      <xsl:element name="{@name}" namespace="{namespace-uri()}"> 
       <def> 
        <xsl:copy-of select="namespace-uri-for-prefix(json)"/> 
       </def> 
       <xsl:value-of select="."/> 
      </xsl:element> 
     </xsl:if> 
    </xsl:template> 
    <xsl:template match="json:string"> 
     <xsl:element name="{../@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:number[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:null[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:boolean[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:boolean[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

我需要的輸出如下:

<Login> 
    <Group> 
     <Group> 
      <Name>john</Name> 
      <Password/> 
     </Group> 
     <Group> 
      <Name>jack</Name> 
      <Password/> 
     </Group> 
    </Group> 
</Login> 

我使用下面的XSL

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://exslt.org/functions" xmlns:xalan="http://xml.apache.org/xslt" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:regexp="http://exslt.org/regular-expressions"> 
    <xsl:output method="text" encoding="utf-8"/> 
    <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())]) &gt; 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::*) &gt; 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::*) &gt; 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(@*) &gt; 0 and (count(child::*) &gt; 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() &lt; 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() &lt; last()">,</xsl:if> 
    </xsl:template> 
    <xsl:template match="node/@TEXT | text()" name="removeBreaks"> 
     <xsl:param name="pText" select="normalize-space(.)"/> 
     <xsl:variable name="myVar" select="regexp:replace($pText,'\\','gi','\\')"/> 
     <xsl:variable name="myVar2" select="regexp:replace($myVar,'&#x22;','gi','\&#x23;')"/> 
     <xsl:variable name="myVar3" select="regexp:replace($myVar2,'&#x2F;','gi','&#x5C;&#x2F;')"/> 
     <xsl:variable name="myVar4" select="regexp:replace($myVar3,'\t','gi','\t')"/> 
     <xsl:variable name="myVar5" select="regexp:replace($myVar4,'\n','gi','\n')"/> 
     <xsl:variable name="myVar6" select="regexp:replace($myVar5,'\r','gi','\r')"/> 
     <xsl:choose> 
      <xsl:when test="not(contains($myVar6, '&#xA;'))"> 
       <xsl:copy-of select="$myVar6"/> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="concat(substring-before($myVar6, '&#xD;&#xA;'), ' ')"/> 
       <xsl:call-template name="removeBreaks"> 
        <xsl:with-param name="pText" select="substring-after($myVar6, '&#xD;&#xA;')"/> 
       </xsl:call-template> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

在這裏我使用模板json:object中的名稱作爲「對象」,但我需要arrayname,arrayname ...等等。請任何人都可以幫我獲得這個輸出嗎?

http://tools.ietf.org/html/draft-rsalz-jsonx-00#page-4 http://controlfreak.net/xml-to-json-in-xslt-a-toolkit/ extracting information from a JSON file using XSLT version 1.0 http://badgerfish.ning.com/

+0

方括號[和]是無效的在XML中的元素名稱。你確定這是你需要的嗎? –

+0

此外,您的輸入XML包含'json:object'元素,您當前的XSLT將其轉換爲'object'元素,但這些元素實際上並未顯示在輸出XML中。那麼你想在'group'或'object'上編號嗎?謝謝! –

+0

編號是不是還有罰款,但我需要的arrayName中和,我們正在使用的數組名是相同的對象.. <! - Nameshould是相同的 - > <! - Nameshould是相同 - > 約翰 約翰 user1731504

回答

1

如果你確定這一點: -

<?xml version="1.0" encoding="utf-8"?> 
<Login> 
    <Group> 
     <Group> 
      <Name>john</Name> 
      <Password/> 
     </Group> 
     <Group> 
      <Name>john</Name> 
      <Password/> 
     </Group> 
    </Group> 
</Login> 

然後以下工作: -

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" exclude-result-prefixes="json"> 
    <xsl:template match="/json:object"> 
     <xsl:apply-templates/> 
    </xsl:template> 
    <xsl:template match="json:array[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:object"> 
     <xsl:element name="{../@name}"> 
      <xsl:apply-templates/> 
     </xsl:element> 
    </xsl:template> 
    <xsl:template match="json:string[@name]"> 
     <xsl:element name="{@name}"> 
      <xsl:value-of select="."/> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

我想以一般的方式指定該名稱,就像數組名稱是用戶一樣,我必須使用與json:object元素名稱相同的名稱aslo – user1731504

+0

已更新,以從父項中選擇名稱。 – Himanshu

+0

@Himanshu缺少模板'match =「json:object [@name]」,它必須爲指定名稱的'object'輸入生成元素。例如,請參閱希望輸出中的「登錄」元素。 – DRCB