Solved

XSL - create a template to avoid redundant code

Posted on 2012-03-28
12
211 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
  • 6
  • 4
  • 2
12 Comments
 
LVL 18

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
 
LVL 18

Assisted Solution

by:zc2
zc2 earned 100 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now