Solved

XSL - create a template to avoid redundant code

Posted on 2012-03-28
12
216 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 52
XML response optional elements 12 80
parsing xml using powershell 6 55
DTD and JAVA versions 1 59
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

752 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