Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Does name exist

From: "Joe Fawcett" <joefawcett@---------.------>
To: NULL
Date: 8/6/2009 5:56:00 PM

"tshad" <toms@p...> wrote in message 
news:#j$ftLrFKHA.3396@T......
>
> "Joe Fawcett" <joefawcett@n...> wrote in message 
> news:O5UvQAoFKHA.4732@T......
>>
>> "tshad" <toms@p...> wrote in message 
>> news:OjA0PNgFKHA.4316@T......
>>> I figured out how to do this by:
>>>
>>>   <source>
>>>      <xsl:if test="../../FIELDS/HEADER">
>>>            <xsl:value-of select="../../FIELDS/HEADER"/>
>>>      </xsl:if>
>>>      <xsl:if test="../../FIELDS/*[name() = 
>>> concat('TITLE',current()/@NUM)]">
>>>             <xsl:value-of select="../../FIELDS/*[name() = 
>>> concat('TITLE',current()/@NUM)]"/>
>>>       </xsl:if>
>>>   </source>
>>>
>>> But what I found that when both exists, it concatenates the 2nd test 
>>> with the first so that I end up with:
>>>
>>>
>>> Subject Photo PageThis is Title1
>>> Subject Photo Page
>>> Subject Photo PageThis is Title3
>>>
>>> How do you tell it to delete what is there (in the 1st and 3rd case) and 
>>> put the new value there.?
>>>
>>> I could put 2 tests in the first part, but I would prefer to write out 
>>> the Headers text and if the 2nd test passes to overwrite what the Header 
>>> test put there.  So I would end up with:
>>>
>>> This is Title1
>>> Subject Photo Page
>>> This is Title3
>>>
>>> Thanks,
>>>
>>> Tom
>>>
>>> "tshad" <toms@p...> wrote in message 
>>> news:uZ9YNkfFKHA.1488@T......
>>>> How would you test if something exists?
>>>>
>>>> If you have:
>>>>
>>>>    <FORM>
>>>>        <FIELDS>
>>>>            <HEADER>Subject Photo Page</HEADER>
>>>>            <TITLE1>This is Title1</TITLE1>
>>>>            <TITLE3>This is Title3</TITLE3>
>>>>        </FIELDS>
>>>>        <FORMPHOTOS>
>>>>            <PHOTO NUM="1">
>>>>                <FILENAME>09May001.jpg</FILENAME>
>>>>                <FILETYPE>JPEG</FILETYPE>
>>>>            </PHOTO>
>>>>            <PHOTO NUM="2">
>>>>                <FILENAME>09005.jpg</FILENAME>
>>>>                <FILETYPE>JPEG</FILETYPE>
>>>>            </PHOTO>
>>>>            <PHOTO NUM="3">
>>>>                <FILENAME>09002.jpg</FILENAME>
>>>>                <FILETYPE>JPEG</FILETYPE>
>>>>            </PHOTO>
>>>>        </FORMPHOTOS>
>>>>    </FORM>
>>>>
>>>> And I want to test if a name exists:
>>>>
>>>> Something like:
>>>>
>>>> <xsl:template match="FORMPHOTOS/PHOTO">
>>>>  <xsl:choose>
>>>>   <xsl:when test=". != ''">
>>>>    <attachment>
>>>>     <source>
>>>>        <xsl:choose>
>>>>
>>>>         <xsl:when test="name() = concat('TITLE',current()/@NUM) 
>>>> exists"> <--------
>>>>
>>>>                    <xsl:value-of select="../../FIELDS/*[name() = 
>>>> concat('TITLE',current()/@NUM)]"/>
>>>>         </xsl:when>
>>>>         <xsl:otherwise>
>>>>                   <xsl:value-of select="../../FIELDS/HEADER"/>
>>>>         </xsl:otherwise>
>>>>        </xsl:choose>
>>>>     </source>
>>>>    </attachment>
>>>>   </xsl:when>
>>>>  </xsl:choose>
>>>> </xsl:template>
>>>>
>>>> What I want to happen here is that for the nodes that have NUM = 1 or 3 
>>>> to use TITLE1 and TITLE3 (since they exist), otherwise use HEADER.
>>>>
>>>> Thanks,
>>>>
>>>> Tom
>>>>
>>>
>>>
>>
>> You could use something like this:
>> <?xml version="1.0" encoding="utf-8"?>
>> <xsl:stylesheet version="1.0" 
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>>    <xsl:output method="xml" indent="yes"/>
>>
>>    <xsl:template match="/">
>>        <photos>
>>            <xsl:apply-templates select="FORM/FORMPHOTOS/PHOTO"/>
>>        </photos>
>>    </xsl:template>
>>
>>  <xsl:template match="PHOTO">
>>    <photo NUM="{@NUM}">
>>      <xsl:variable name="titleElementName" select="concat('TITLE', @NUM)" 
>> />
>>      <xsl:choose>
>>        <xsl:when test="/FORM/FIELDS/*[local-name() = $titleElementName]">
>>          <xsl:value-of select="/FORM/FIELDS/*[local-name() = 
>> $titleElementName]" />
>>        </xsl:when>
>>        <xsl:otherwise>
>>          <xsl:value-of select="/FORM/FIELDS/HEADER" />
>>        </xsl:otherwise>
>>      </xsl:choose>
>>    </photo>
>>  </xsl:template>
>>
>> </xsl:stylesheet>
>>
>> The XML schema is poor, having elements like TITLE1, TITLE2 etc will 
>> always make processing difficult, better to use attributes:
>> <TITLE number="1" />
>> for example.
>
> That looks good.
>
> I agree on the TITLE1 and TITLE2 issue but the XML that I am processing is 
> not mine so I have no control over the names of the elements.
>
> I did find another way to solve the checking if a name exists:
>
>      <xsl:if test="../../FIELDS/HEADER">
>       <xsl:value-of select="../../FIELDS/HEADER"/>
>      </xsl:if>
>      <xsl:if test="../../FIELDS/*[name() = 
> concat('TITLE',current()/@NUM)]">
>       <xsl:value-of select="../../FIELDS/*[name() = 
> concat('TITLE',current()/@NUM)]"/>
>      </xsl:if>
>
> This one works fine but if HEADER exists and TITLEx exists, then they will 
> concatenate.
>
> That was the other question I had, how to get the 2nd test to wipe out 
> whatever is in the field instead of concatenating. The above could end up 
> with:
>
> Subject Photo PageThis is Title1
>
> instead of:
>
> This is Title1
>
> Because HEADER existed.
>
> I solved this problem by using a CHOOSE instead of IF.
>
>      <xsl:choose>
>         <xsl:when test="../../FIELDS/*[name() = 
> concat('TITLE',current()/@NUM)]">
>            <xsl:value-of select="../../FIELDS/*[name() = 
> concat('TITLE',current()/@NUM)]"/>
>         </xsl:when>
>         <xsl:when test="../../FIELDS/HEADER">
>             <xsl:value-of select="../../FIELDS/HEADER"/>
>         </xsl:when>
>      </xsl:choose>
>
> In this case Header will not happen if TITLEx is found.
>
> But is there a way to make the work where by default HEADER will get 
> written out and if TITLEx is found, it will overwrite what was in HEADER 
> instead of just adding to it.
>
>      <xsl:value-of select="../../FIELDS/HEADER"/>
>      <xsl:if test="../../FIELDS/*[name() = 
> concat('TITLE',current()/@NUM)]">
>           <xsl:value-of select="../../FIELDS/*[name() = 
> concat('TITLE',current()/@NUM)]"/>
>      </xsl:if>
>
> Thanks,
>
> Tom
>>
>> Regards
>>
>> -- 
>>
>> Joe Fawcett (MVP - XML)
>> http://joe.fawcett.name
>
>

I don't quite understand, my stylesheet outputs the relevant TITLEx if it's 
there, otherwise the HEADER element.

-- 

Joe Fawcett (MVP - XML)
http://joe.fawcett.name 



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent