2016-11-30 5 views
0

入力XMLにヘッダー、コンテンツ、フッターの各部分があります。 XMLからJSONへの変換は、XSLTを使用するとうまくいきます。XSLTを使用して1つのXMLファイルから3つのJSONに分割する方法

<header> 
    <trackingSettings> 
    <urlcode>W3333</urlcode> 
    <apiurl>http://mlucenter.com/like/api</apiurl> 
    </trackingSettings> 
</header> 
<mlu3_body> 
    <columnsCount>2</columnsCount> 
    <lineBackground>linear-gradient(to right, rgba(94, 172, 192, 0) 0%, c4cccf 50%, rgba(94, 172, 192, 0) 100%)</lineBackground> 
</mlu3_body> 
<footer> 
    <buttons> 
    <button/> 
    </buttons> 
    <banner/> 
</footer> 

私のXSLT使用:サクソンPE内を使用して

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes" method="xml" /> 

     <xsl:template match="*"> 
      <xsl:value-of select="name()"/> : <xsl:call-template name="Properties"/> 
     </xsl:template> 

     <xsl:template match="*" mode="ArrayElement"> 
      <xsl:call-template name="Properties"/> 
     </xsl:template> 

     <xsl:template name="Properties"> 
      <xsl:variable name="childName" select="name(*[1])"/> 
      <xsl:choose> 
       <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when> 
       <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when> 
       <xsl:otherwise>{ 
        <xsl:apply-templates select="@*"/> 
        <xsl:apply-templates select="*"/> 
        }</xsl:otherwise> 
      </xsl:choose> 
      <xsl:if test="following-sibling::*">,</xsl:if> 
     </xsl:template> 

     <xsl:template match="@*">"<xsl:value-of select="name()"/>" : '<xsl:value-of select="."/>', 
     </xsl:template> 
</xsl:stylesheet> 
ここ

イム

私の入力XMLファイルは次のとおりです。しかし、私は、ヘッダー、コンテンツやフッターなど三つの部分として出力する必要があります酸素:

このXMLをheader.json,content.json(mlu3_body)とfooter.jsonの3つのJSONファイルに変換します。

これはXSLTを使用することで可能ですか、すべての入力ファイルを個別に保持したいですか?いくつか考えてください。

+0

「XMLからJSONへの変換がXSLTを使用してうまく機能する」場合は、そのコードを表示してください。一般に、Saxon 9でoXygenでサポートされているXSLT 2.0と3.0は、もちろんxsl:result-documentというhttps://www.w3.org/TR/xslt20/#creating-result-treesを使っていくつかの結果ドキュメントを書くことができます。 –

+0

XSLTで編集しました。@ Martin – User501

答えて

0

変更

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 

     <xsl:template match="*"> 
      <xsl:value-of select="name()"/> : <xsl:call-template name="Properties"/> 
     </xsl:template> 

     <xsl:template match="*" mode="ArrayElement"> 
      <xsl:call-template name="Properties"/> 
     </xsl:template> 

     <xsl:template name="Properties"> 
      <xsl:variable name="childName" select="name(*[1])"/> 
      <xsl:choose> 
       <xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when> 
       <xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" mode="ArrayElement"/>] }</xsl:when> 
       <xsl:otherwise>{ 
        <xsl:apply-templates select="@*"/> 
        <xsl:apply-templates select="*"/> 
        }</xsl:otherwise> 
      </xsl:choose> 
      <xsl:if test="following-sibling::*">,</xsl:if> 
     </xsl:template> 

     <xsl:template match="@*">"<xsl:value-of select="name()"/>" : '<xsl:value-of select="."/>', 
     </xsl:template> 

     <xsl:template match="header"> 
      <xsl:result-document href="header.json"> 
      <xsl:next-match/> 
      </xsl:result-document> 
     </xsl:template> 

     <xsl:template match="mlu3_body"> 
      <xsl:result-document href="content.json"> 
      <xsl:next-match/> 
      </xsl:result-document> 
     </xsl:template> 

     <xsl:template match="footer"> 
      <xsl:result-document href="footer.json"> 
      <xsl:next-match/> 
      </xsl:result-document> 
     </xsl:template> 


</xsl:stylesheet> 

にXSLT、それはこれらの3つの要素のための3つの結果ファイルを生成する必要があります。あなたの質問を編集し、生産された内容がまだ適切でない場合には、どんな結果が必要かを正確に教えてください。表示されたXMLのスニペットが、より大きな文書の一部であるかどうかは、明確ではありません。

+0

酸素パラメータ出力タブの変換シナリオで何かを変更する必要がありますか? – User501

+0

なぜoXygenで何かを変更する必要があるのか​​分かりません。 –

+0

そのうまい@Martin。ありがとう – User501

関連する問題