Hi Buddy,

I have a requirement to convert the below shown input.xml into result.xml and the XSLT which I am trying with is shown at the end.

input.xml:

<match>
    <FieldMatcherResult>
        <FieldPair>
            <Context>A</Context>
            <Field1>A1</Field1>
            <Field2>A2</Field2>
        </FieldPair>
        <FieldPair>
            <Context>B</Context>
            <Field1>B1</Field1>
            <Field2>B2</Field2>
        </FieldPair>
    </FieldMatcherResult>
</match>

result.xml:

<result>
<FieldMatcherResult>
        <FieldPair>
            <Context>A</Context>
            <Field1>A1</Field1>
            <Field2>A2</Field2>
        </FieldPair>
</FieldMatcherResult>
<FieldMatcherResult>
        <FieldPair>
            <Context>B</Context>
            <Field1>B1</Field1>
            <Field2>B2</Field2>
        </FieldPair>
</FieldMatcherResult>
<FieldMatcherResult>
 </result>

I have written transformation like:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="FieldMatcherResult">
    <result>
      <xsl:for-each select="FieldPair">
        <FieldMatcherResult>
          <xsl:copy-of select="." />
        </FieldMatcherResult>
      </xsl:for-each>
    </result>
  </xsl:template>
</xsl:stylesheet>

But it's not working............simply throwing java.util.EmptyStackException(I am using the XSLT inside a java based application).....Can anybody help me where I am doing wrong in the transformation......

Any help would be greatly appreciated......

Thank you...!!!

The provided XSLT actually runs cleanly for me in VS2010 XSLT debugging.

However, you are using XSLT like it is a programatical language which it is not, I used to make this mistake also. Below I have provided a remake of it which removes the programatical style, as there was no need for the for-each.

XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output indent ="yes" method="xml"/>
  <xsl:strip-space elements ="*"/>

  <xsl:template match="FieldMatcherResult">
    <xsl:element name ="result">
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>

  <xsl:template match="FieldPair">
        <FieldMatcherResult>
          <xsl:copy-of select="." />
        </FieldMatcherResult>
  </xsl:template>

</xsl:stylesheet>
INPUT
<match>
  <FieldMatcherResult>
    <FieldPair>
      <Context>A</Context>
      <Field1>A1</Field1>
      <Field2>A2</Field2>
    </FieldPair>
    <FieldPair>
      <Context>B</Context>
      <Field1>B1</Field1>
      <Field2>B2</Field2>
    </FieldPair>
  </FieldMatcherResult>
</match>
OUTPUT
<?xml version="1.0" encoding="utf-8"?>
<result>
  <FieldMatcherResult>
    <FieldPair>
      <Context>A</Context>
      <Field1>A1</Field1>
      <Field2>A2</Field2>
    </FieldPair>
  </FieldMatcherResult>
  <FieldMatcherResult>
    <FieldPair>
      <Context>B</Context>
      <Field1>B1</Field1>
      <Field2>B2</Field2>
    </FieldPair>
  </FieldMatcherResult>
</result>

If the error still occurs I think it will be something else in the Java code and not the actual XSLT.

Hope that helps.

Hi 'MikeyIsMe',
Thank you very much for your response. I'll try this on Monday.
Have a nice weekend..

Ok let me know how it goes :)

Thank you, have a nice one also!

Hi,

As per your explanation, I tried the XSLT code in the below shown manner. My idea is that as there are a number of 'FieldPair' elements in the input xml file, the template gets instantiated the same number of times and thus we need not go with 'for-each'. But, the code is throwing the same error as "java.util.EmptyStackException". I was wondering whether the error is because of some customisations done in my java app. I have pasted my complete input xml file and the expected output file.
Request you to please throw some light on the xslt template instantiation procedure. So far, I was thinking that once the default built-in template(match='/') gets instantiated, the processor looks for all the templates matching the child elements and the output tree is framed accordingly.
Is it like all the template matches written in xslt should be as per the xml elements order appearing in the input xml file? For example, for the given input xml, if I write an XSLT template match='FieldPairs', the processor keeps on scanning the input xml and looks for any template matching the current node. so, obviously this template with match='FieldPairs' should get instantiated when the current node is at FieldPairs. Right? But this is also throwing the same error.

Thank you very much for your hearing of my problem.

INPUT:

<FieldMatcherResult>
    <MaximumNumberOfResults>1</MaximumNumberOfResults>
    <FieldMatcherResultElements>
        <FieldMatcherResultElement>
            <Total>
                <Result>M</Result>
                <Aggregated>100.0</Aggregated>
                <UnMatchedThreshold>81.0</UnMatchedThreshold>
                <OriginalStatus/>
                <Manual>N</Manual>
                <ExternalReferenceID/>
            </Total>
            <Detail>
                <FieldPairs>
                    <FieldPair>
                        <Context>DTBTPE.DBSSSGSG.P</Context>
                        <Field1>1</Field1>
                        <Field2>1</Field2>
                        <Description>15A</Description>
                        <Result>M</Result>
                        <ResultWeight>0.0</ResultWeight>
                        <ToleranceType>None</ToleranceType>
                        <MatchWeight>0.0</MatchWeight>
                    </FieldPair>
                    <FieldPair>
                        <Context>DTBTPE.DBSSSGSG.R</Context>
                        <Field1>FXD0044458081-1</Field1>
                        <Field2>FXD0044457801-1</Field2>
                        <Description>21: Pre Reference</Description>
                        <Result>UM</Result>
                        <ResultWeight>0.0</ResultWeight>
                        <ToleranceType>None</ToleranceType>
                        <MatchWeight>0.0</MatchWeight>
                    </FieldPair>
                    <FieldPair>
                        <Context/>
                        <Field1>DBSSSG0771DBSSTP</Field1>
                        <Field2>DBSSSG0771DBSSTP</Field2>
                        <Description>22C: Common Reference</Description>
                        <Result>M</Result>
                        <ResultWeight>20.0</ResultWeight>
                        <ToleranceType>None</ToleranceType>
                        <MatchWeight>20.0</MatchWeight>
                    </FieldPair>
                </FieldPairs>
            </Detail>
        </FieldMatcherResultElement>
    </FieldMatcherResultElements>
</FieldMatcherResult>

XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/FieldMatcherResult/FieldMatcherResultElements/FieldMatcherResultElement/Detail/FieldPairs/FieldPair">
        <result>
            <xsl:copy-of select="."/>
        </result>
    </xsl:template>
</xsl:stylesheet>

OUTPUT:

<?xml version="1.0" encoding="utf-8"?>
<result>
    <FieldMatcherResult>
        <FieldPair>
                    <Context>DTBTPE.DBSSSGSG.P</Context>
                        <Field1>1</Field1>
                        <Field2>1</Field2>
                        <Description>15A</Description>
                        <Result>M</Result>
                        <ResultWeight>0.0</ResultWeight>
                        <ToleranceType>None</ToleranceType>
                        <MatchWeight>0.0</MatchWeight>
        </FieldPair>
    </FieldMatcherResult>
    <FieldMatcherResult>
                        <Context>DTBTPE.DBSSSGSG.R</Context>
                        <Field1>FXD0044458081-1</Field1>
                        <Field2>FXD0044457801-1</Field2>
                        <Description>21: Pre Reference</Description>
                        <Result>UM</Result>
                        <ResultWeight>0.0</ResultWeight>
                        <ToleranceType>None</ToleranceType>
                        <MatchWeight>0.0</MatchWeight>
    </FieldMatcherResult>
    <FieldMatcherResult>
                        <FieldPair>
                        <Context/>
                        <Field1>DBSSSG0771DBSSTP</Field1>
                        <Field2>DBSSSG0771DBSSTP</Field2>
                        <Description>22C: Common Reference</Description>
                        <Result>M</Result>
                        <ResultWeight>20.0</ResultWeight>
                        <ToleranceType>None</ToleranceType>
                        <MatchWeight>20.0</MatchWeight>
                    </FieldPair>
    </FieldMatcherResult>
</result>

As a devoted XML/XSL developer I thought I would pay homage to the java.util.EmptyStackException, which in english terms means that you’re performing a transform with a stylesheet that’s not valid XML or otherwise building some DOM model with invalid XML…

[Quoted from here]

In terms of how the templates work, they simply match any node with the same name as what the template is specified to match, and it will match all occurances of that node.

It doesnt matter what order they are in within the stylesheet as it will try to match all templates against each node until one, if any, is found.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.