Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSL - create a template to avoid redundant code

Posted on 2012-03-28
12
Medium Priority
?
218 Views
Last Modified: 2012-03-28
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>
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 19

Expert Comment

by:zc2
ID: 37777297
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37777371
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
 

Author Comment

by:badtz7229
ID: 37777383
how would i call this template?
is it         <xsl:apply-templates select="Main[Carrier/Code]">
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 19

Assisted Solution

by:zc2
zc2 earned 400 total points
ID: 37777410
<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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37777430
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37777477
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
 

Author Comment

by:badtz7229
ID: 37777542
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37777579
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
 

Author Comment

by:badtz7229
ID: 37778030
"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
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1600 total points
ID: 37778069
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
 

Author Closing Comment

by:badtz7229
ID: 37778240
thank you both
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 37778259
welcome
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question