Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

xslt - Need help in writing common xsl

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...