2

I have an xml as:

<?xml version="1.0" encoding="UTF-16"?><SiebelMessage
 MessageId="1-2WPBF"
 IntObjectName="LHC Rules Items IO"
 MessageType="Integration Object"
 IntObjectFormat="Siebel Hierarchical"
><ListOfLhcRulesItemsBo
><LhcRuleItems
><Comments
>Pick@Unique</Comments
><Level
>3</Level
><Id
>1-2BCN3B</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Bench Type</Rule
><SubCategory
>Pauper</SubCategory
><Value
>Divisional</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Unique</Comments
><Level
>3</Level
><Id
>1-2BFEH7</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Court Fee</Rule
><SubCategory
>Pauper</SubCategory
><Value
>1</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>2</Level
><Id
>1-2BIJY8</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Bench Type</Rule
><SubCategory
></SubCategory
><Value
>Single</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>2</Level
><Id
>1-2BIJY9</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Court Fee</Rule
><SubCategory
></SubCategory
><Value
>200</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>1</Level
><Id
>1-2BIRER</Id
><Branch
>Civil</Branch
><Category
></Category
><Rule
>Bench Type</Rule
><SubCategory
></SubCategory
><Value
>Single</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Ignore</Comments
><Level
>1</Level
><Id
>1-2BIRET</Id
><Branch
>Civil</Branch
><Category
></Category
><Rule
>Court Fee</Rule
><SubCategory
></SubCategory
><Value
>100</Value
></LhcRuleItems
><LhcRuleItems
><Comments
>Pick@Unique</Comments
><Level
>2</Level
><Id
>1-2BISGB</Id
><Branch
>Civil</Branch
><Category
>C.M. (Civil)</Category
><Rule
>Adjournment Duration</Rule
><SubCategory
></SubCategory
><Value
>2</Value
></LhcRuleItems
><LhcRuleItems
><Comments
></Comments
><Level
>1</Level
><Id
>1-2BS0UV</Id
><Branch
>Civil</Branch
><Category
></Category
><Rule
>Bench Rules</Rule
><SubCategory
></SubCategory
><Value
>BR</Value
></LhcRuleItems
></ListOfLhcRulesItemsBo
></SiebelMessage
>

I want output like:

<Rules>
<BenchType>Divisional</BenchType>
<CourtFee>1</CourtFee>
</Rules>

I am tying a solution posted in this postLink but it is not working. I am getting output with values only. Current output:

<?xml version="1.0" encoding="UTF-8"?>
<SiebelMessage MessageId="1-2WPBG" IntObjectName="LHC Rules Items IO" MessageType="Integration Object" IntObjectFormat="Siebel Hierarchical">
<ListOfLhcRulesItemsBo>




</ListOfLhcRulesItemsBo>
</SiebelMessage>

Attempted XSLT:

<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
    <xsl:key name="UniqueRecords" match="LhcRuleItems" use="Rule"/>
    <xsl:template match="node()|@*" name="FinalRules">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="LhcRuleItems"/>
<xsl:template match="SiebelMessage/ListOfLhcRulesItemsBo/LhcRuleItems">
        <xsl:for-each select="Rule">
            <xsl:element name="{translate(Rule, ' ' , '')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Updated question with a complete sample xml file. I updated my current XSLT and now I am not getting anything in output.

Community
  • 1
  • 1
Bill
  • 172
  • 8

1 Answers1

2

The reason why your attempt cannot work is that you are trying to make "Bench Type" and "Court Fee" into element names - but element names cannot contain a space.

If you change:

<xsl:element name="{Rule}">

to:

<xsl:element name="{translate(Rule, ' ' , '')}">

you will get a result, when applied to the shown example. Other values might not be valid element names for different reasons.


Note also that:

<xsl:value-of select="Rule"/>

should be:

<xsl:value-of select="Value"/>

Added:

In your new attempt, you need to change:

<xsl:template match="SiebelMessage/ListOfLhcRulesItemsBo/LhcRuleItems">
        <xsl:for-each select="Rule">
            <xsl:element name="{translate(Rule, ' ' , '')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
</xsl:template>

to:

<xsl:template match="SiebelMessage/ListOfLhcRulesItemsBo/LhcRuleItems">
    <xsl:element name="{translate(Rule, ' ' , '')}">
        <xsl:value-of select="Value"/>
    </xsl:element>
</xsl:template>

i.e. remove the xsl:for-each instruction you have added - which wasn't there in your original question.

You have too much code anyway. You have defined a key which isn't used at all, and you have two conflicting templates matching the same LhcRuleItems element. All you need to do to get the specified output is:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/SiebelMessage">
    <Rules>
        <xsl:for-each select="ListOfLhcRulesItemsBo/LhcRuleItems">
            <xsl:element name="{translate(Rule, ' ' , '')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>  
    </Rules>
</xsl:template>

</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
  • Thank you. I updated my question as per your mentioned instructions but now I am not getting anything as output. – Bill Jun 06 '16 at 15:28