XSL - create a template to avoid redundant code

I have a snippet of code that I'd like to place in a template so that it can work dynamically. The xml can be in one of 2 formats:
"Main/Carrier/Code" or "../MainInfo/Main/Carrier/Code"
How do I reference either case in one template, so that I don't use redundant code?

I would need the "value-of  select=" (from below) to be dynamic.

      <xsl:choose>
        <xsl:when test="count(Main/Carrier/Code) != 0">        
      <xsl:variable name="tcode"   select="Main/Carrier/Code" />
      <Carrier>
        <Code>
          <xsl:value-of  select="$tcode"/>
        </Code>
        <AName>
          <xsl:value-of  select="Main/Carrier/AName"/>
        </AName>
      </Carrier>
        </xsl:when>
        <xsl:otherwise>
          <Carrier>
            <Code>
              <xsl:value-of  select="Main/MCarrier/Code"/>
            </Code>
            <AName>
              <xsl:value-of  select="Main/Carrier/Text"/>
            </AName>
          </Carrier>
        </xsl:otherwise>
      </xsl:choose>
badtz7229Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zc2Commented:
you could apply a filter pattern to the template, so it will be called only on a element
"Main" which has a child Carrier which has a child Code.
Both your cases are matched to that template.

<xsl:template match="Main[Carrier/Code]">
... template body
</xsl:template>
0
Geert BormansInformation ArchitectCommented:
No need to put that in a named template, having a seperate matching template is always a good thing.
There is a whole lot of things I would do differently,
but I would prefer to see the XML before I rewrite

If I understand the logic well, you use the Carrier node if it has a Code element
and you use the MCarrier node if there isn't a Carrier/Code?
In the second case is the Carrier/Text OK, or should it be MCarrier/Text?

Then, can there be more than one Carrier/Code? If so the value-of is weird.
I would definitely make sure that this piece of code runs in the context of Main, since all has Main to start

so instead of this code do <xsl:apply-templates select="Main"/> (maybe with a mode?

and have a main template

<xsl:template match="Main">
      <xsl:choose>
        <xsl:when test="Carrier/Code">        
      <xsl:variable name="tcode"   select="Carrier/Code" />
      <Carrier>
        <Code>
          <xsl:value-of  select="$tcode"/>
        </Code>
        <AName>
          <xsl:value-of  select="Carrier/AName"/>
        </AName>
      </Carrier>
        </xsl:when>
        <xsl:otherwise>
          <Carrier>
            <Code>
              <xsl:value-of  select="MCarrier/Code"/>
            </Code>
            <AName>
              <xsl:value-of  select="Carrier/Text"/>
            </AName>
          </Carrier>
        </xsl:otherwise>
      </xsl:choose></xsl:template>

Open in new window


note that I removed the count. If there is no slection in the XPath, it will render false (empty nodesets in a boolean expression render false)
note that I moved the context in all XPath

you can get there from mainInfo then too

I also don't see the /MainInfo play in your current stylesheet actually
0
badtz7229Author Commented:
how would i call this template?
is it         <xsl:apply-templates select="Main[Carrier/Code]">
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

zc2Commented:
<xsl:apply-templates select="Main">
in the first case and
<xsl:apply-templates select="../MainInfo/Main">
in the second

use the template body as Gertone suggests (i.e. without the Main in all the selects)
0
Geert BormansInformation ArchitectCommented:
Actually, here is how I would rerwrite the XSLT

    <xsl:template match="/">
        <xsl:apply-templates select="//Main"/>
    </xsl:template>
    
    <xsl:template match="Main">
        <Carrier>
            <xsl:apply-templates select="Carrier[Code]"/>
            <xsl:apply-templates select="MCarrier[not(../Carrier/Code)]"/>
        </Carrier>
    </xsl:template>
    
    <xsl:template match="Carrier">
        <xsl:copy-of  select="Code"/>
        <xsl:copy-of  select="AName"/>
    </xsl:template>
 
    <xsl:template match="MCarrier">
        <xsl:copy-of  select="Code"/>
        <AName>
            <xsl:value-of  select="../Carrier/Text"/>
        </AName>
    </xsl:template>

Open in new window

0
Geert BormansInformation ArchitectCommented:
was rewriting and missed the follow up posts

ignore the first three lines of my "new" stylesheet and use
<xsl:apply-templates select="Main">
in the first case and
<xsl:apply-templates select="../MainInfo/Main">
in the second

as zc2 suggests

As you can see in boths zc2s first post and my latest, it is always a good thing to let the XPath predicates play instead of using choose constructs

this could be found weird
           
 <xsl:apply-templates select="Carrier[Code]"/>
            <xsl:apply-templates select="MCarrier[not(../Carrier/Code)]"/>

Open in new window


it pushes out a Carrier node if it has a code child
and it pushes out an MCarrier node if the sibling Carrier has no Code
actually only one of the two will have an actual content... hence your choose is tricked in
0
badtz7229Author Commented:
When I call the template using
<xsl:apply-templates select="Main"> and debug - this sends me to an autogenerated from the stylesheet which then grabs the correct data but with no xml syntax.
0
Geert BormansInformation ArchitectCommented:
well, the issue with templates is that though they provide a nice seperation of code, they can conflict.
You can use "mode" to avoid teh conflicts
Whilst advicing we don't know how many templates already are in your stylesheet
So maybe you can share both source XML and XSLT or add modes to all our suggestions
0
badtz7229Author Commented:
"Main/Carrier/Code" or "../MainInfo/Main/Carrier/Code"

xml code
<A>
      <RQ>
            <Test>NA</Test>
      </RQ>
      <MainInfo>  <!-- this node always exists-->
            <Main>
                  <MCarrier>
                        <Code>ST</Code>
                        <AName>STUDENT</AName>
                  </MCarrier>
                  <Carrier>
                        <Text>SUBSIDIARY</Text>
                  </Carrier>
            </Main>
      </MainInfo>
      <INPUT></INPUT>      <!-- the xml may or may not contain this node-->
</A>

xsl code for first case. Currently, this code executes when <MainInfo> is present
    <xsl:choose>
        <xsl:when test="count(Main/Carrier/Code) != 0">        
      <xsl:variable name="tcode"   select="Main/Carrier/Code" />
      <Carrier>
        <Code>
          <xsl:value-of  select="$tcode"/>
        </Code>
        <AName>
          <xsl:value-of  select="Main/Carrier/AName"/>
        </AName>
      </Carrier>
        </xsl:when>
        <xsl:otherwise>
          <Carrier>
            <Code>
              <xsl:value-of  select="Main/MCarrier/Code"/>
            </Code>
            <AName>
              <xsl:value-of  select="Main/Carrier/Text"/>
            </AName>
          </Carrier>
        </xsl:otherwise>
      </xsl:choose>
        
xsl code for second case. Currently, this code executes when <INPUT> is present

    <xsl:choose>
        <xsl:when test="count(../MainInfo/Main/Carrier/Code) != 0">        
      <xsl:variable name="tcode"   select="../MainInfo/Main/Carrier/Code" />
      <Carrier>
        <Code>
          <xsl:value-of  select="$tcode"/>
        </Code>
        <AName>
          <xsl:value-of  select="../MainInfo/Main/Carrier/AName"/>
        </AName>
      </Carrier>
        </xsl:when>
        <xsl:otherwise>
          <Carrier>
            <Code>
              <xsl:value-of  select="../MainInfo/Main/MCarrier/Code"/>
            </Code>
            <AName>
              <xsl:value-of  select="../MainInfo/Main/Carrier/Text"/>
            </AName>
          </Carrier>
        </xsl:otherwise>
      </xsl:choose>
0
Geert BormansInformation ArchitectCommented:
replace the first choose code with this
<xsl:apply-templates select="Main" mode="carrier">

replace the second choose code with this
<xsl:apply-templates select="../MainInfo/Main" mode="carrier">

and add these templates to the stylesheet

   <xsl:template match="Main"  mode="carrier">
        <Carrier>
            <xsl:apply-templates select="Carrier[Code]"  mode="carrier"/>
            <xsl:apply-templates select="MCarrier[not(../Carrier/Code)]"  mode="carrier"/>
        </Carrier>
    </xsl:template>
    
    <xsl:template match="Carrier"  mode="carrier">
        <xsl:copy-of  select="Code"/>
        <xsl:copy-of  select="AName"/>
    </xsl:template>
 
    <xsl:template match="MCarrier"  mode="carrier">
        <xsl:copy-of  select="Code"/>
        <AName>
            <xsl:value-of  select="../Carrier/Text"/>
        </AName>
    </xsl:template>

Open in new window


it will do exactly the same as the two choose constructs and now this is shielded from other templates by the mode attribute
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badtz7229Author Commented:
thank you both
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.