1

I got an XML file that's using very generic XML tag names but a rich attribute structure such as: <Field Name="CRSDESC1" FieldName="{@CRS_DESC}"><FormattedValue>Survey of Human Anatomy and Physiology</FormattedValue></Field>

I want to change the existing element name as shown above by using the "Name" attribute, to this: <CRSDESC1>Survey of Human Anatomy and Physiology</CRSDESC1>

Once we create the new element, we no longer need the element <FormattedValue> nor the duplicate <Value>

Here is a sample section of the XML:

<CrystalReport> <ReportHeader><Details Level="1"> <Section SectionNumber="0"> <Field Name="CRSDESC1" FieldName="{@CRS_DESC}"><FormattedValue>Heating and Air Contracting</FormattedValue><Value>Heating and Air Contracting</Value></Field> <Field Name="CRSTITLE1" FieldName="{@CRS_TITLE}"><FormattedValue>Heating and Air Conditioning Contracting</FormattedValue><Value>Heating and Air Conditioning Contracting</Value></Field> <Field Name="CRSDEPTS1" FieldName="{Command.CRS_DEPTS}"><FormattedValue>AIRC</FormattedValue><Value>AIRC</Value></Field> <Field Name="DEPTSDESC1" FieldName="{Command.DEPTS_DESC}"><FormattedValue>Heating, A/C &amp; Refrigeration</FormattedValue><Value>Heating, A/C &amp; Refrigeration</Value></Field> <Field Name="CRSNO1" FieldName="{Command.CRS_NO}"><FormattedValue>2260</FormattedValue><Value>2260</Value></Field> <Field Name="CRSSTANDARDARTICULATIONNO1" FieldName="{Command.CRS_STANDARD_ARTICULATION_NO}"><FormattedValue></FormattedValue><Value></Value></Field> <Field Name="CRSMINCRED1" FieldName="{Command.CRS_MIN_CRED}"><FormattedValue>3.00</FormattedValue><Value>3.00</Value></Field> </Section> </Details> <Details Level="1"> <Section SectionNumber="0"> <Field Name="CRSDESC1" FieldName="{@CRS_DESC}"><FormattedValue>Survey of Human Anatomy and Physiology</FormattedValue><Value>Survey of Human Anatomy and Physiology</Value></Field> <Field Name="CRSTITLE1" FieldName="{@CRS_TITLE}"><FormattedValue>Survey of Human Anatomy and Physiology</FormattedValue><Value>Survey of Human Anatomy and Physiology</Value></Field> <Field Name="CRSDEPTS1" FieldName="{Command.CRS_DEPTS}"><FormattedValue>ANAT</FormattedValue><Value>ANAT</Value></Field> <Field Name="DEPTSDESC1" FieldName="{Command.DEPTS_DESC}"><FormattedValue>Anatomy &amp; Physiology</FormattedValue><Value>Anatomy &amp; Physiology</Value></Field> <Field Name="CRSNO1" FieldName="{Command.CRS_NO}"><FormattedValue>1500</FormattedValue><Value>1500</Value></Field> <Field Name="CRSSTANDARDARTICULATIONNO1" FieldName="{Command.CRS_STANDARD_ARTICULATION_NO}"><FormattedValue>L1 904L</FormattedValue><Value>L1 904L</Value></Field> <Field Name="CRSMINCRED1" FieldName="{Command.CRS_MIN_CRED}"><FormattedValue>4.00</FormattedValue><Value>4.00</Value></Field> </Section> </Details> <ReportHeader><CrystalReport>

1 Answer 1

2

This would work...

XML Input (corrected to be well-formed)

<CrystalReport>
    <ReportHeader>
        <Details Level="1">
            <Section SectionNumber="0">
                <Field Name="CRSDESC1" FieldName="{@CRS_DESC}">
                    <FormattedValue>Heating and Air Contracting</FormattedValue>
                    <Value>Heating and Air Contracting</Value>
                </Field>
                <Field Name="CRSTITLE1" FieldName="{@CRS_TITLE}">
                    <FormattedValue>Heating and Air Conditioning Contracting</FormattedValue>
                    <Value>Heating and Air Conditioning Contracting</Value>
                </Field>
                <Field Name="CRSDEPTS1" FieldName="{Command.CRS_DEPTS}">
                    <FormattedValue>AIRC</FormattedValue>
                    <Value>AIRC</Value>
                </Field>
                <Field Name="DEPTSDESC1" FieldName="{Command.DEPTS_DESC}">
                    <FormattedValue>Heating, A/C &amp; Refrigeration</FormattedValue>
                    <Value>Heating, A/C &amp; Refrigeration</Value>
                </Field>
                <Field Name="CRSNO1" FieldName="{Command.CRS_NO}">
                    <FormattedValue>2260</FormattedValue>
                    <Value>2260</Value>
                </Field>
                <Field Name="CRSSTANDARDARTICULATIONNO1" FieldName="{Command.CRS_STANDARD_ARTICULATION_NO}">
                    <FormattedValue/>
                    <Value/>
                </Field>
                <Field Name="CRSMINCRED1" FieldName="{Command.CRS_MIN_CRED}">
                    <FormattedValue>3.00</FormattedValue>
                    <Value>3.00</Value>
                </Field>
            </Section>
        </Details>
        <Details Level="1">
            <Section SectionNumber="0">
                <Field Name="CRSDESC1" FieldName="{@CRS_DESC}">
                    <FormattedValue>Survey of Human Anatomy and Physiology</FormattedValue>
                    <Value>Survey of Human Anatomy and Physiology</Value>
                </Field>
                <Field Name="CRSTITLE1" FieldName="{@CRS_TITLE}">
                    <FormattedValue>Survey of Human Anatomy and Physiology</FormattedValue>
                    <Value>Survey of Human Anatomy and Physiology</Value>
                </Field>
                <Field Name="CRSDEPTS1" FieldName="{Command.CRS_DEPTS}">
                    <FormattedValue>ANAT</FormattedValue>
                    <Value>ANAT</Value>
                </Field>
                <Field Name="DEPTSDESC1" FieldName="{Command.DEPTS_DESC}">
                    <FormattedValue>Anatomy &amp; Physiology</FormattedValue>
                    <Value>Anatomy &amp; Physiology</Value>
                </Field>
                <Field Name="CRSNO1" FieldName="{Command.CRS_NO}">
                    <FormattedValue>1500</FormattedValue>
                    <Value>1500</Value>
                </Field>
                <Field Name="CRSSTANDARDARTICULATIONNO1" FieldName="{Command.CRS_STANDARD_ARTICULATION_NO}">
                    <FormattedValue>L1 904L</FormattedValue>
                    <Value>L1 904L</Value>
                </Field>
                <Field Name="CRSMINCRED1" FieldName="{Command.CRS_MIN_CRED}">
                    <FormattedValue>4.00</FormattedValue>
                    <Value>4.00</Value>
                </Field>
            </Section>
        </Details>
    </ReportHeader>
</CrystalReport>

XSLT 1.0

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

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

    <xsl:template match="Field[@Name]">
        <xsl:element name="{@Name}">
            <xsl:value-of select="FormattedValue"/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

XML Output

<CrystalReport>
   <ReportHeader>
      <Details Level="1">
         <Section SectionNumber="0">
            <CRSDESC1>Heating and Air Contracting</CRSDESC1>
            <CRSTITLE1>Heating and Air Conditioning Contracting</CRSTITLE1>
            <CRSDEPTS1>AIRC</CRSDEPTS1>
            <DEPTSDESC1>Heating, A/C &amp; Refrigeration</DEPTSDESC1>
            <CRSNO1>2260</CRSNO1>
            <CRSSTANDARDARTICULATIONNO1/>
            <CRSMINCRED1>3.00</CRSMINCRED1>
         </Section>
      </Details>
      <Details Level="1">
         <Section SectionNumber="0">
            <CRSDESC1>Survey of Human Anatomy and Physiology</CRSDESC1>
            <CRSTITLE1>Survey of Human Anatomy and Physiology</CRSTITLE1>
            <CRSDEPTS1>ANAT</CRSDEPTS1>
            <DEPTSDESC1>Anatomy &amp; Physiology</DEPTSDESC1>
            <CRSNO1>1500</CRSNO1>
            <CRSSTANDARDARTICULATIONNO1>L1 904L</CRSSTANDARDARTICULATIONNO1>
            <CRSMINCRED1>4.00</CRSMINCRED1>
         </Section>
      </Details>
   </ReportHeader>
</CrystalReport>
Sign up to request clarification or add additional context in comments.

4 Comments

This isn't working for me. Can you give me any suggestions why it wouldn't work for me? I'm still seeing all the original tag names and structure.
I ran your XSLT against the sample data you revised and it worked fine. What was the problem you fixed in the data to make it well-formed? I don't see an issue in the data off-hand
OK, I see that you moved the end of </ReportHeader> to the end of the XML. That's not how it appears in the actual XML. There is a <ReportHeader> and a <ReportFooter> in the actual XML. When I deleted the DTD in my actual XML file your XSL functioned properly. Thanks
@JimMaivald - What I did was change the last 2 tags (<ReportHeader><CrystalReport>) to </ReportHeader></CrystalReport>. Glad you got it figured out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.