[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

xsl: build a dynamic tag based on parsing string that I get from XML file using xsl

I  need to be able to build a dynamic tag based on parsing string that I get from XML file using xsl. For  example,
1.      Parse a pipe delimited string value1|type|value2
2.       If type = XYZ1
then  < ABC  name1=value2 name2=type>
                            value1
           </ABC>
3.      If type = XYZ2
then  < ABC  name1=value2 name2=type
                  value1
 </ABC>

0
jgordin
Asked:
jgordin
  • 5
  • 3
1 Solution
 
jgordinAuthor Commented:
correction:
2.   If type = XYZ1
then  < ABC  name1="value2" name2="type">
                            value1
           </ABC>
3.      If type = XYZ2
then  < ABC  name1="value2" name2="type"
                  value1
0
 
Geert BormansCommented:
I see no difference between the first condition and the second

Can you use XSLT2.0?
0
 
jgordinAuthor Commented:
this is what i use:

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:str="http://exslt.org/strings" exclude-result-prefixes ="str">

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!

 
Geert BormansCommented:
Assuming that you don't need the test and there are only two '|'
(I have stuffed the string in  <text> element)

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="text">
        <ABC name1="{substring-after(substring-after(., '|'), '|')}" name2="{substring-before(substring-after(., '|'), '|')}">
            <xsl:value-of select="substring-before(., '|')" />
        </ABC>
    </xsl:template>
</xsl:stylesheet>
0
 
jgordinAuthor Commented:
the only difference between first and second conditions is type.
0
 
jgordinAuthor Commented:
Gertone:
let me test your solution.

thanks.
0
 
jgordinAuthor Commented:
how do i pass a string that i need to match?

that is, i do xsl:value-of select="xyz5", where xyz5 is a tag in the XML file that i parse. the select statement returns string value1|type|value2. thanks.
0
 
Geert BormansCommented:
make it an apply-templates
<xsl:apply-templates select="xyz5"/>

the node will then be passed to the template for xyz5

then you can put the above code in the template for "xyz5", like this
 
<xsl:template match="xyz5">
        <ABC name1="{substring-after(substring-after(., '|'), '|')}" name2="{substring-before(substring-after(., '|'), '|')}">
            <xsl:value-of select="substring-before(., '|')" />
        </ABC>
    </xsl:template>
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now