Question

This is in continuation of my previous questions (sorry for re-posting similar type of question again):

Merge functionality of two xsl files into a single file (not a xsl import or include issue)

and

Merge functionality of two xsl files into a single file (continued.....)

This is actually a bit manipulation of my second question. I now need to merge the solution provided by Flack to my first question with a "choose" condition in my xsl:

<xsl:choose>
          <xsl:when test='/Declaration/Header/DeclarantsReference = ""'>
            <DeclarantsReference>
              <xsl:text disable-output-escaping="no">A</xsl:text>
            </DeclarantsReference>
          </xsl:when>
          <xsl:otherwise>
            <DeclarantsReference>
              <xsl:value-of select="/Declaration/Header/DeclarantsReference"/>
            </DeclarantsReference>
          </xsl:otherwise>
        </xsl:choose>

Now any sample xml input like:

    <Declaration>
         <Message>
            <Meduim>#+#</Meduim>
            <CommonAccessReference></CommonAccessReference>
         </Message>
         <BeginingOfMessage>
            <MessageCode>5</MessageCode>
            <DeclarationCurrency></DeclarationCurrency>
            <MessageFunction>ISD</MessageFunction>
         </BeginingOfMessage>
         <Header>
            <DeclarantsReference></DeclarantsReference>
            <Items>
            <Documents>
                  <ItemDocument>
                     <DocumentCode>XXX</DocumentCode>
                     <DocumentPart></DocumentPart>
                     <DocumentLanguage>#+#</DocumentLanguage>
                  </ItemDocument>
               </Documents>
            </Items>
           </Header>
</Declaration>

should output:

<Declaration>
 <Message>
  <Meduim></Meduim>
 </Message>
 <BeginingOfMessage>
  <MessageCode>5</MessageCode>
  <MessageFunction>ISD</MessageFunction>
 </BeginingOfMessage>
 <Header>
 <DeclarantsReference>A</DeclarantsReference>
  <Items>
   <Documents>
    <ItemDocument>
     <DocumentCode>XXX</DocumentCode>
     <DocumentLanguage></DocumentLanguage>
    </ItemDocument>
   </Documents>
  </Items>
 </Header>
</Declaration>

Thanks for any help in advance.

Was it helpful?

Solution

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*[not(node())]"/>
    <xsl:template match="text()" name="strip">
        <xsl:param name="pString" select="."/>
        <xsl:param name="pOutput" select="substring-before($pString,'#+#')"/>
        <xsl:choose>
            <xsl:when test="contains($pString,'#+#')">
                <xsl:call-template name="strip">
                    <xsl:with-param name="pString"
                                    select="substring-after($pString,'#+#')"/>
                    <xsl:with-param name="pOutput"
                                    select="concat($pOutput,
                                                   substring-before($pString,
                                                                    '#+#'))"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat($pOutput,$pString)"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="DeclarantsReference[not(node())]"
                  priority="1">
        <xsl:copy>A</xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output:

<Declaration>
    <Message>
        <Meduim></Meduim>
    </Message>
    <BeginingOfMessage>
        <MessageCode>5</MessageCode>
        <MessageFunction>ISD</MessageFunction>
    </BeginingOfMessage>
    <Header>
        <DeclarantsReference>A</DeclarantsReference>
        <Items>
            <Documents>
                <ItemDocument>
                    <DocumentCode>XXX</DocumentCode>
                    <DocumentLanguage></DocumentLanguage>
                </ItemDocument>
            </Documents>
        </Items>
    </Header>
</Declaration>

Note: Rules overwriting the identity rule.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top