Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Combining elements [Thread Next] Re: Combining elementsTo: NULL Date: 4/12/2008 1:26:00 PM
tshad wrote:
> "Martin Honnen" <mahotrash@y...> wrote in message
> news:eoQPgH$kIHA.6032@T......
>> tshad wrote:
>>
>>> What I would like the result to look like is:
>>> ************************************************************
>>> <?xml version="1.0" encoding="utf-8"?>
>>> <REPORT>
>>> <form>
>>> <sectionNumber>0</sectionNumber>
>>> <primary>True</primary>
>>> <formName>1004</formName>
>>> <tagName>OTHERFILENUMBER</tagName>
>>> <flags>0</flags>
>>> <format>0</format>
>>> <value>692</value>
>>> </form>
>>> <form>
>>> <sectionNumber>0</sectionNumber>
>>> <primary>True</primary>
>>> <formName>1004</formName>
>>> <tagName>FNMA_FILENUMBER</tagName>
>>> <flags>0</flags>
>>> <format>0</format>
>>> <value>693</value>
>>> </form>
>>> <form>
>>> <sectionNumber>0</sectionNumber>
>>> <primary>True</primary>
>>> <formName>1004</formName>
>>> <tagName>SUBPROPADDRESS</tagName>
>>> <flags>0</flags>
>>> <format>0</format>
>>> <value>3</value>
>>> </form>
>>> <form>
>>> <sectionNumber>0</sectionNumber>
>>> <primary>True</primary>
>>> <formName>1004</formName>
>>> <tagName>SCMCOMMENTS_1</tagName>
>>> <flags>0</flags>
>>> <format>0</format>
>>> <value>This is a test line 1 This is a test line 2 This is a test
>>> line 3 This is a test line 4</value>
>>> </form>
>>> <form>
>>> <sectionNumber>1</sectionNumber>
>>> <primary>True</primary>
>>> <formName>1004</formName>
>>> <tagName>FormFormats</tagName>
>>> <flags>0</flags>
>>> <format>0</format>
>>> <value>NUM=1 FORMCODE=1004 SECCODE=1 DESC= MAJOR=True</value>
>>> </form>
>>> </REPORT>
>> Here is an adapted stylesheet that creates the output described above:
>>
>> <xsl:stylesheet
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> version="1.0">
>>
>> <xsl:output method="xml" indent="yes"/>
>>
>> <xsl:template match="REPORT">
>> <xsl:copy>
>> <xsl:apply-templates
>> select="FORMS/FORM/FIELDS/*[not(starts-with(name(), 'SCMCOMMENTS'))]"/>
>> <form>
>> <sectionNumber>0</sectionNumber>
>> <primary>True</primary>
>> <formName><xsl:value-of select="FORMS/FORM/@FORMCODE"/></formName>
>> <tagName>SCMCOMMENTS_1</tagName>
>> <flags>0</flags>
>> <format>0</format>
>> <value>
>> <xsl:apply-templates select="FORMS/FORM/FIELDS/*[starts-with(name(),
>> 'SCMCOMMENTS')]"/>
>> </value>
>> </form>
>> <form>
>> <sectionNumber>
>> <xsl:value-of select="FORMS/FORM/@SECCODE"/>
>> </sectionNumber>
>> <primary>True</primary>
>> <formName>
>> <xsl:value-of select="FORMS/FORM/@FORMCODE"/>
>> </formName>
>> <tagName>FormFormats</tagName>
>> <flags>0</flags>
>> <format>0</format>
>> <value>
>> <xsl:apply-templates select="FORMS/FORM/@*"/>
>> </value>
>> </form>
>> <xsl:apply-templates select="FORMS/FORM/attachments/attachment"/>
>> </xsl:copy>
>> </xsl:template>
>>
>> <xsl:template match="FIELDS/*[not(starts-with(name(), 'SCMCOMMENTS'))]">
>> <form>
>> <sectionNumber>
>> <xsl:text>0</xsl:text>
>> </sectionNumber>
>> <primary>True</primary>
>> <formName>
>> <xsl:value-of select="../../@FORMCODE"/>
>> </formName>
>> <tagName>
>> <xsl:value-of select="name()"/>
>> </tagName>
>> <flags>0</flags>
>> <format>0</format>
>> <value>
>> <xsl:value-of select="."/>
>> </value>
>> </form>
>> </xsl:template>
>>
>> <xsl:template match="FIELDS/*[starts-with(name(), 'SCMCOMMENTS')]">
>> <xsl:value-of select="."/>
>> <xsl:if test="position() != last()">
>> <xsl:text> </xsl:text>
>> </xsl:if>
>> </xsl:template>
>>
>> <xsl:template match="FORM/@*">
>> <xsl:value-of select="concat(name(), '=', .)"/>
>> <xsl:if test="position() != last()">
>> <xsl:text> </xsl:text>
>> </xsl:if>
>> </xsl:template>
>>
>> </xsl:stylesheet>
>>
>>
>
> How would I modify the above script to just look at the <FORM> that has
> @MAJOR = TRUE?
>
> I have the following xml:
>
> The sample xml file that looks almost the same as my other one but also has
> another FORM in it that doesn't have @MAJOR = true and it looks something
> like:
> *********************************************************
> <?xml version="1.0" encoding="utf-8"?>
> <REPORT VERSION="1.10" FILENUM="" DESCRIPTION="Form Utility XML: 3/18/2008
> 12:27:13 PM" MAJORFORM="1004">
> <FORMS>
> <FORM NUM="1" FORMCODE="1004" SECCODE="1" DESC="" MAJOR="True" >
> <FIELDS>
> <OTHERFILENUMBER>692</OTHERFILENUMBER>
> <FNMA_FILENUMBER>693</FNMA_FILENUMBER>
> <SUBPROPADDRESS>3</SUBPROPADDRESS>
> <SCMCOMMENTS_1>This is a test line 1</SCMCOMMENTS_1>
> <SCMCOMMENTS_2>This is a test line 2</SCMCOMMENTS_2>
> <SCMCOMMENTS_3>This is a test line 3</SCMCOMMENTS_3>
> <SCMCOMMENTS_4>This is a test line 4</SCMCOMMENTS_4>
> </FIELDS>
> </FORM>
> <FORM NUM="1" FORMCODE="GA1" MAJOR="False" FONTNAME="Arial" >
> <FIELDS>
> <FOOTER1>Form GA1 - "WinTOTAL" appraisal software by a la mode,
> inc. - 1-800-ALAMODE</FOOTER1>
> <SUBPROPADDRESS>10 KENSINGTON CT</SUBPROPADDRESS>
> <SUBLEGALDESCRIP>BLOCK-2006 LOT- 6.0205 MSA -
> 35644</SUBLEGALDESCRIP>
> <SUBCOMBADDRESS>TENAFLY, NJ 07670-2872</SUBCOMBADDRESS>
> <SUBLENCLIENT>CITI HOME EQUITY</SUBLENCLIENT>
> </FIELDS
> </Form>
> </FORMS>
> </REPORT>
> How would I change your sheet to only look the ones with @major = true? Or
> apply all the same rules as before only to this one. Then handle the 2nd
> Form separately, say a test on @FORMCODE="GA1"
If you only want to process certain elements then select those with
xsl:apply-templates e.g.
<xsl:apply-templates
select="FORMS/FORM[@MAJOR='True']/FIELDS/*[not(starts-with(name(),
'SCMCOMMENTS'))]"/>
Things get more complicated if you want to process the other FORM
elements as well as then you have to make sure you can distinguish the
different elements.
There are (at least) two ways to solve that, one is using modes e.g. you use
<xsl:apply-templates
select="FORMS/FORM[@MAJOR='True']/FIELDS/*[not(starts-with(name(),
'SCMCOMMENTS'))]" mode="mode1"/>
and then write matching templates for that mode e.g.
<xsl:template match="FIELDS/*[not(starts-with(name(),
'SCMCOMMENTS'))]" mode="mode1">
then you use a second mode for the other elements.
Or you need to write templates that distinguish the elements based on
the match pattern.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
