Groups > Design > Microsoft xsl > Re: Combining elements




Combining elements

Combining elements
Mon, 31 Mar 2008 18:59:45 -070
I have a file that can have comments that are spread among mulitple elements 
in various formats (comment1,comment2, comment3...) or (comment_1,comment2, 
comment3...) or (comment, comment1, comment2...)

I need to concatenate them in my xslt sheet.

I already have a sheet that works, and need to add this in.  If it is a 
problem, then I will just do it in my DataSet - but I want to get as much 
done with the xslt sheet as possible.

I have the following xslt that I have stripped down that works real well 
that Martin helped me put together:

**********************************************************
<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/*"/>
   <xsl:apply-templates
select="FORMS/FORM/attachments/attachment"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="FIELDS/*">
  <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:if test="position()=last()">
   <form>
    <sectionNumber>
     <xsl:value-of select="../../@SECCODE"/>
    </sectionNumber>
    <primary>True</primary>
    <formName>
     <xsl:value-of select="../../@FORMCODE"/>
    </formName>
    <tagName>FormFormats</tagName>
    <flags>0</flags>
    <format>0</format>
    <value>
     <xsl:apply-templates select="../../@*"/>
    </value>
   </form>
  </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>
**********************************************************

To start out with I would like to do is test the <tagName> and if it ends

with a "_1" and if so loop through the next siblings to see if there
are any 
more that start with the same name (minus the number) and concatenate them. 
That would be too hard.  But how do I tell it not to ignore those that I 
have concatenated?  I don't want to change the structure too much as it 
works now for a variety of types of elements.

The sample xml file looks 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>
 </FORMS>
</REPORT>
*********************************************************

This file creates this result:
*********************************************************
<?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</value>
  </form>
  <form>
    <sectionNumber>0</sectionNumber>
    <primary>True</primary>
    <formName>1004</formName>
    <tagName>SCMCOMMENTS_2</tagName>
    <flags>0</flags>
    <format>0</format>
    <value>This is a test line 2</value>
  </form>
  <form>
    <sectionNumber>0</sectionNumber>
    <primary>True</primary>
    <formName>1004</formName>
    <tagName>SCMCOMMENTS_3</tagName>
    <flags>0</flags>
    <format>0</format>
    <value>This is a test line 3</value>
  </form>
  <form>
    <sectionNumber>0</sectionNumber>
    <primary>True</primary>
    <formName>1004</formName>
    <tagName>SCMCOMMENTS_4</tagName>
    <flags>0</flags>
    <format>0</format>
    <value>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>
*********************************************************

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>
*******************************************************

There could be 3 comments or there could be 15.

Thanks,

Tom 

Post Reply
Re: Combining elements
Tue, 1 Apr 2008 10:31:58 -0700
"Martin Honnen" <mahotrash@yahoo.de> wrote in message 
news:eoQPgH$kIHA.6032@TK2MSFTNGP03.phx.gbl...
> 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>
>
>
This works perfect.

I am just not sure I understand why. as normally we don't do anything in the 
first template except apply the next templates.  This has always been a 
confusion with me on how the different templates work.  I got a better idea 
when we did the concat of the attributes and put the apply-templates inside 
the 2nd template.

In all my other ones, I always used the 1st template to apply the other 
templates, but did nothing in it.

This one is different.

I'll take a stab at what I think it is doing.

1) We get all the elements in the page.  Since REPORT is the first element - 
we get that and all below it.  We could also have done "/*", I believe
- not 
sure about "/".

2) Not sure what the xsl:copy does, but then we apply-template that looks 
similar to "FORMS/FORM/FIELDS/*[not(starts-with(name(),
'SCMCOMMENTS'))]". 
The template actually is "FIELDS/*[not(starts-with(name(),
'SCMCOMMENTS'))]" 
which gets all the elements that don't start with SCMCOMMENTS and applys the 
template to each of those fields. Not sure why the one has FORMS/FORM/ in 
front of it and the other doesn't.

3) Then we do one element set that starts with SCMCOMMENTS and for the value 
we apply the template that looks like: 
FORMS/FORM/FIELDS/*[starts-with(name(), 'SCMCOMMENTS')], but doesn't have 
the FORMS/FORM/ in front and we keep adding them together with a blank at 
the end - if not the last one.

4) Then we do one set that concatenates the attributes of the FORM element 
together.

5) Lastly, we apply the attachment elements (which we don't have in this xml 
file) but does work fine in the finished one.

This works fine with one set of comments, but is there a way to change the 
"FIELDS/*[not(starts-with(name(), 'SCMCOMMENTS'))]" to something that
looks 
at multiple sets of comments.  For example, if we had a file that looked 
something like:

The sample xml file looks 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>
    <GEN_COMMENTS_1>Testing line 1</GEN_COMMENTS_1>
    <GEN_COMMENTS_2>Testing line 2</GEN_COMMENTS_2>
    <GEN_COMMENTS_3>Testing line 3</GEN_COMMENTS_3>
    <GEN_COMMENTS_4>Testing line 4</GEN_COMMENTS_4>
    <CMPHIST_TXT>The test line1</CMPHIST_TXT>
    <CMPHIST_TXT2 >The test line1</CMPHIST_TXT2>
    <CMPHIST_TXT3 >The test line1</CMPHIST_TXT3>
    <CMPHIST_TXT4 >The test line1</CMPHIST_TXT4>
</FIELDS>
  </FORM>
 </FORMS>
</REPORT>
*********************************************************

Could we change the select statements to something like:

FIELDS/*[not(starts-with(name(), 'SCMCOMMENTS')) & not(starts-with(name(), 
'GEN_COMMENTS')) & not(starts-with(name(), 'CMPHIST_TXT')) ]"

and then set up different templates for each.

Or do some type of contains that compares multiple strings?

Thanks,

Tom
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/ 

Post Reply
Re: Combining elements
Tue, 1 Apr 2008 11:22:28 -0700
"tshad" <tshad@dslextreme.com> wrote in message 
news:OUvUz6BlIHA.2368@TK2MSFTNGP03.phx.gbl...
>
> "Martin Honnen" <mahotrash@yahoo.de> wrote in message 
> news:eoQPgH$kIHA.6032@TK2MSFTNGP03.phx.gbl...
>> 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>
>>
>>
> This works perfect.
>
> I am just not sure I understand why. as normally we don't do anything in 
> the first template except apply the next templates.  This has always been 
> a confusion with me on how the different templates work.  I got a better 
> idea when we did the concat of the attributes and put the apply-templates 
> inside the 2nd template.
>
> In all my other ones, I always used the 1st template to apply the other 
> templates, but did nothing in it.
>
> This one is different.
>
> I'll take a stab at what I think it is doing.
>
> 1) We get all the elements in the page.  Since REPORT is the first 
> element - we get that and all below it.  We could also have done
"/*", I 
> believe - not sure about "/".
>
> 2) Not sure what the xsl:copy does, but then we apply-template that looks 
> similar to "FORMS/FORM/FIELDS/*[not(starts-with(name(),
'SCMCOMMENTS'))]". 
> The template actually is "FIELDS/*[not(starts-with(name(), 
> 'SCMCOMMENTS'))]" which gets all the elements that don't start with 
> SCMCOMMENTS and applys the template to each of those fields. Not sure why 
> the one has FORMS/FORM/ in front of it and the other doesn't.
>
> 3) Then we do one element set that starts with SCMCOMMENTS and for the 
> value we apply the template that looks like: 
> FORMS/FORM/FIELDS/*[starts-with(name(), 'SCMCOMMENTS')], but doesn't have 
> the FORMS/FORM/ in front and we keep adding them together with a blank at 
> the end - if not the last one.
>
> 4) Then we do one set that concatenates the attributes of the FORM element

> together.
>
> 5) Lastly, we apply the attachment elements (which we don't have in this 
> xml file) but does work fine in the finished one.
>
> This works fine with one set of comments, but is there a way to change the

> "FIELDS/*[not(starts-with(name(), 'SCMCOMMENTS'))]" to something
that 
> looks at multiple sets of comments.  For example, if we had a file that 
> looked something like:
>
> The sample xml file looks 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>
>    <GEN_COMMENTS_1>Testing line 1</GEN_COMMENTS_1>
>    <GEN_COMMENTS_2>Testing line 2</GEN_COMMENTS_2>
>    <GEN_COMMENTS_3>Testing line 3</GEN_COMMENTS_3>
>    <GEN_COMMENTS_4>Testing line 4</GEN_COMMENTS_4>
>    <CMPHIST_TXT>The test line1</CMPHIST_TXT>
>    <CMPHIST_TXT2 >The test line1</CMPHIST_TXT2>
>    <CMPHIST_TXT3 >The test line1</CMPHIST_TXT3>
>    <CMPHIST_TXT4 >The test line1</CMPHIST_TXT4>
> </FIELDS>
>  </FORM>
> </FORMS>
> </REPORT>
> *********************************************************
>
> Could we change the select statements to something like:
>
> FIELDS/*[not(starts-with(name(), 'SCMCOMMENTS')) &
not(starts-with(name(), 
> 'GEN_COMMENTS')) & not(starts-with(name(), 'CMPHIST_TXT')) ]"
>
> and then set up different templates for each.
>
> Or do some type of contains that compares multiple strings?
>

I was able to put together a script that worked with the multiple groups of 
comments that works pretty well.  Not sure if the best way, especially since 
I could have 20 or 30 groups of comments.  But a start.

Here is the script I did:
************************************************
<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')) and 
not(starts-with(name(), 'GEN_COMMENTS')) and not(starts-with(name(), 
'CMPHIST_TXT'))]"/>
   <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>0</sectionNumber>
    <primary>True</primary>
    <formName>
     <xsl:value-of select="FORMS/FORM/@FORMCODE"/>
    </formName>
    <tagName>GEN_COMMENTS_1</tagName>
    <flags>0</flags>
    <format>0</format>
    <value>
     <xsl:apply-templates
select="FORMS/FORM/FIELDS/*[starts-with(name(), 
'GEN_COMMENTS')]"/>
    </value>
   </form>
   <form>
    <sectionNumber>0</sectionNumber>
    <primary>True</primary>
    <formName>
     <xsl:value-of select="FORMS/FORM/@FORMCODE"/>
    </formName>
    <tagName>CMPHIST_TXT</tagName>
    <flags>0</flags>
    <format>0</format>
    <value>
     <xsl:apply-templates
select="FORMS/FORM/FIELDS/*[starts-with(name(), 
'CMPHIST_TXT')]"/>
    </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'))
and 
not(starts-with(name(), 'GEN_COMMENTS')) and not(starts-with(name(), 
'CMPHIST_TXT'))]">
  <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="FIELDS/*[starts-with(name(),
'GEN_COMMENTS')]">
  <xsl:value-of select="."/>
  <xsl:if test="position() != last()">
   <xsl:text> </xsl:text>
  </xsl:if>
 </xsl:template>
 <xsl:template match="FIELDS/*[starts-with(name(),
'CMPHIST_TXT')]">
  <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>
************************************************

Thanks for all the help,

Tom

> Thanks,
>
> Tom
>> -- 
>>
>> Martin Honnen --- MVP XML
>> http://JavaScript.FAQTs.com/
>
> 

Post Reply
Re: Combining elements
Tue, 01 Apr 2008 14:13:57 +020
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>


-- 

	Martin Honnen --- MVP XML
Post Reply
Re: Combining elements
Fri, 11 Apr 2008 10:41:10 -070
"Martin Honnen" <mahotrash@yahoo.de> wrote in message 
news:eoQPgH$kIHA.6032@TK2MSFTNGP03.phx.gbl...
> 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"

Thanks,

Tom
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/ 

Post Reply
<< Previous 1 2 Next >>
( Page 1 of 2 )
about | contact