Need help in writing common xsl

佐手、 提交于 2019-12-14 03:35:31

问题


I have to generate XMl output. It should display arrays as shown later. I am unable to render the arrays in the proper way.

Input XML:

<accounts>
    <displayOrdinal>0</displayOrdinal>
    <name>String</name>
    <account>
        <accountNumber>String</accountNumber>
        <name>String</name>
        <balance>
            <balanceAmount>0.0</balanceAmount>
        </balance>
        <balance>
            <balanceAmount>0.0</balanceAmount>
        </balance>
        <properties>
            <displayOrdinal>0</displayOrdinal>
        </properties>
        <properties>
            <displayOrdinal>0</displayOrdinal>
        </properties>
        <usage>
            <type>String</type>
        </usage>
        <usage>
            <type>String</type>
        </usage>
    </account>
    <account>
        <accountNumber>String</accountNumber>
        <name>String</name>
        <balance>
            <balanceAmount>0.0</balanceAmount>
        </balance>
        <balance>
            <balanceAmount>0.0</balanceAmount>
        </balance>
        <properties>
            <displayOrdinal>0</displayOrdinal>
        </properties>
        <properties>
            <displayOrdinal>0</displayOrdinal>
        </properties>
        <usage>
            <type>String</type>
        </usage>
        <usage>
            <type>String</type>
        </usage>
    </account>
</accounts>

My expected output should be as follows:

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <json:object name="accounts">
        <json:string name="displayOrdinal">0</json:string>
        <json:string name="name">String</json:string>
        <json:array name="account">
            <json:object>
                <json:string name="accountNumber">String</json:string>
                <json:string name="name">String</json:string>
                <json:array name="balance">
                    <json:object>
                        <json:string name="balanceAmount">0.0</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="balanceAmount">0.0</json:string>
                    </json:object>
                </json:array>
                <json:array name="properties">
                    <json:object>
                        <json:string name="displayOrdinal">0</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="displayOrdinal">0</json:string>
                    </json:object>
                </json:array>
                <json:array name="usage">
                    <json:object>
                        <json:string name="type">String</json:string>
                    </json:object>
                    <json:object name="usage">
                        <json:string name="type">String</json:string>
                    </json:object>
                </json:array>
            </json:object>
            <json:object>
                <json:string name="accountNumber">String</json:string>
                <json:string name="name">String</json:string>
                <json:object name="balance">
                    <json:string name="balanceAmount">0.0</json:string>
                </json:object>
                <json:array name="balance">
                    <json:object>
                        <json:string name="balanceAmount">0.0</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="displayOrdinal">0</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="displayOrdinal">0</json:string>
                    </json:object>
                </json:array>
                <json:array name="usage">
                    <json:object>
                        <json:string name="type">String</json:string>
                    </json:object>
                    <json:object>
                        <json:string name="type">String</json:string>
                    </json:object>
                </json:array>
            </json:object>
        </json:array>
    </json:object>
</json:object>

The XSL that I am using is as below:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <!-- Array -->
    <xsl:template match="*[*[2]][name(*[1])=name(*[2])]">
        <json:object name="{name()}">
            <json:array name="{name(*[1])}">
                <xsl:apply-templates/>
            </json:array>
        </json:object>
    </xsl:template>
    <!-- Array member -->
    <xsl:template match="*[parent::*[ name(*[1])=name(*[2]) ]] | /">
        <json:object>
            <xsl:apply-templates/>
        </json:object>
    </xsl:template>
    <!-- Object -->
    <xsl:template match="*">
        <xsl:choose>
            <xsl:when test="text()">
                <json:string name="{name()}">
                    <xsl:value-of select="."/>
                </json:string>
            </xsl:when>
            <xsl:otherwise>
                <json:object name="{name()}">
                    <xsl:apply-templates/>
                </json:object>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <!-- String -->
    <xsl:template match="*[not(*)]">
        <xsl:choose>
            <xsl:when test="not(boolean(text()))">
                <xsl:element name="json:null">
                    <xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:choose>
                    <xsl:when test="text()= 'false' or text()='true'">
                        <xsl:element name="json:boolean">
                            <xsl:attribute name="name"><xsl:value-of select="name()"/></xsl:attribute>
                            <xsl:value-of select="text()"/>
                        </xsl:element>
                    </xsl:when>
                    <xsl:otherwise>
                        <json:string name="{name()}">
                            <xsl:if test="@*">
                                <xsl:attribute name="{name(@*)}"><xsl:value-of select="@*"/></xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="."/>
                        </json:string>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

回答1:


I think you will need a number of templates for this. Firstly, for the array elements, you will need to match elements who have different preceding sibling, but a matching-named following sibling (i.e. they are the first element of the array)

<xsl:template 
      match="*
         [local-name() != local-name(preceding-sibling::*[1])]
         [local-name() = local-name(following-sibling::*[1])]" priority="2">
   <json:array name="{local-name()}">
      <xsl:apply-templates select="self::*" mode="array" />
   </json:array>
</xsl:template>

The priority is used here because in the final XSLT, there will be two templates that could potential match the same element, and the array one needs to be chosen first.

Then you can process the elements in the array by outputing the element as an json object, and then matching the following sibling but only if it has the name name.

<xsl:template match="*" mode="array">
   <json:object>
      <xsl:apply-templates />
   </json:object>
   <xsl:apply-templates select="following-sibling::*[1][local-name() = local-name(current())]" mode="array" />
</xsl:template>

There would also need to be a template to stop elements in the array being matched independently of the array processing so that the don't get output twice.

<xsl:template match="*[local-name() = local-name(preceding-sibling::*[1])]" />

The other templates needed will be one for string elements, which are elements with no child elements

<xsl:template match="*[not(*)]" priority="1">
   <json:string name="{local-name()}">
      <xsl:value-of select="." />
   </json:string>      
</xsl:template> 

And finally one to match other elements, which would just be output as objects.

<xsl:template match="*">
   <json:object name="{local-name()}">
      <xsl:apply-templates />
   </json:object>      
</xsl:template>

Here is the full XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
      <json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
         <xsl:apply-templates />
      </json:object>
   </xsl:template>

   <xsl:template match="*[local-name() != local-name(preceding-sibling::*[1])][local-name() = local-name(following-sibling::*[1])]" priority="2">
      <json:array name="{local-name()}">
         <xsl:apply-templates select="self::*" mode="array" />
      </json:array>
   </xsl:template>

   <xsl:template match="*" mode="array">
      <json:object>
         <xsl:apply-templates />
      </json:object>
      <xsl:apply-templates select="following-sibling::*[1][local-name() = local-name(current())]" mode="array" />
   </xsl:template>

   <xsl:template match="*[local-name() = local-name(preceding-sibling::*[1])]" />

   <xsl:template match="*[not(*)]" priority="1">
      <json:string name="{local-name()}">
         <xsl:value-of select="." />
      </json:string>      
   </xsl:template>   

   <xsl:template match="*">
      <json:object name="{local-name()}">
         <xsl:apply-templates />
      </json:object>      
   </xsl:template>

   <xsl:template match="@*|node()[not(self::*)]">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

When applied to your XML, the following is output

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
   <json:object name="accounts">
      <json:string name="displayOrdinal">0</json:string>
      <json:string name="name">String</json:string>
      <json:array name="account">
         <json:object>
            <json:string name="accountNumber">String</json:string>
            <json:string name="name">String</json:string>
            <json:array name="balance">
               <json:object>
                  <json:string name="balanceAmount">0.0</json:string>
               </json:object>
               <json:object>
                  <json:string name="balanceAmount">0.0</json:string>
               </json:object>
            </json:array>
            <json:array name="properties">
               <json:object>
                  <json:string name="displayOrdinal">0</json:string>
               </json:object>
               <json:object>
                  <json:string name="displayOrdinal">0</json:string>
               </json:object>
            </json:array>
            <json:array name="usage">
               <json:object>
                  <json:string name="type">String</json:string>
               </json:object>
               <json:object>
                  <json:string name="type">String</json:string>
               </json:object>
            </json:array>
         </json:object>
         <json:object>
            <json:string name="accountNumber">String</json:string>
            <json:string name="name">String</json:string>
            <json:array name="balance">
               <json:object>
                  <json:string name="balanceAmount">0.0</json:string>
               </json:object>
               <json:object>
                  <json:string name="balanceAmount">0.0</json:string>
               </json:object>
            </json:array>
            <json:array name="properties">
               <json:object>
                  <json:string name="displayOrdinal">0</json:string>
               </json:object>
               <json:object>
                  <json:string name="displayOrdinal">0</json:string>
               </json:object>
            </json:array>
            <json:array name="usage">
               <json:object>
                  <json:string name="type">String</json:string>
               </json:object>
               <json:object>
                  <json:string name="type">String</json:string>
               </json:object>
            </json:array>
         </json:object>
      </json:array>
   </json:object>
</json:object>



回答2:


Congrats to Tim for first correct solution.

My solution is not any better, but I present it as a matter of interest. It produces the same output as Tim's, plus I have incorporated the null and boolean types suggested by the OP's original solution attempt.

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
  exclude-result-prefixes="xsl xs">
<xsl:output indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" /> 

<xsl:template match="/" name="nameless-object">
  <json:object>
    <xsl:apply-templates />    
  </json:object>
</xsl:template>

<!-- Array -->
<xsl:template match="*
  [     following-sibling::*[1][name()=name(preceding-sibling::*[1])] ]
  [not( preceding-sibling::*[1][name()=name(following-sibling::*[1])])]">
  <xsl:variable name="array-name" select="name()" />  
  <xsl:variable name="followers" select="
    (.|following-sibling::*)[name()=$array-name]" />
  <xsl:variable name="stop" select="
     (following-sibling::*[name()!=$array-name][1] |
      ../*[last()])[1]" />
  <xsl:variable name="preceders" select="
    ($stop| $stop/preceding-sibling::*)[name()=$array-name]" />
  <xsl:variable name="members" select="
      $preceders[count(.|$followers)=count($followers)]" />
  <json:array name="{$array-name}">
    <xsl:for-each select="$members">
      <xsl:call-template name="nameless-object" />
    </xsl:for-each>  
  </json:array>
</xsl:template>

<xsl:template match="*
    [preceding-sibling::*[1][name()=name(following-sibling::*[1])] ]" />

<!-- Object -->  
<xsl:template match="*">
  <json:object name="{name()}">
    <xsl:apply-templates />
  </json:object>
</xsl:template>

<!-- String -->
<xsl:template match="*[not(*)]">
  <json:string name="{name()}">
    <xsl:value-of select="." />
  </json:string>
</xsl:template>

<!-- Null -->
<xsl:template match="*[not(*)][.='']">
  <json:null name="{name()}" />
</xsl:template>

<!-- Boolean -->
<xsl:template match="*[not(*)][.='true' or .='false']">
  <json:boolean name="{name()}">
    <xsl:value-of select="." />
  </json:boolean>
</xsl:template>

</xsl:stylesheet>


来源:https://stackoverflow.com/questions/13375360/need-help-in-writing-common-xsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!