-2

I have a nested hierarchial xml structure which is to be flattened using xsl transformation. Following is the scenario.

<company>
  <Managers>
    <Manager>
       <Name>Matt</Name>
       <ID>1</ID>
       <Manager>
          <Name>Joe</Name>
          <ID>11</ID>
          <Manager>
          <Name>Dave</Name>
          <ID>111</ID>
       </Manager>
       </Manager>
    </Manager>
    <Manager>
        <Name>mike</Name>
          <ID>2</ID>>
    </Manager>
  </Managers>
</company>

result:

Matt 1
Joe 11
Dave 111
Mike 2
3
  • 1
    So have you tried anything? Read any tutorials? What is your difficulty, exactly? Commented Apr 1, 2015 at 14:52
  • Hmm. Yeah. I kinda assumed the question was what's the proper XPath. The XSLT is pretty trivial. In the future, it's best to post a sample of what you've attempted Commented Apr 1, 2015 at 14:55
  • Please don't do tag spamming and restrict to tags directly related. Commented Apr 1, 2015 at 16:16

1 Answer 1

2

A better alternative via @Mathias Mueller,

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match="Manager">
        <xsl:value-of select="Name" />
        <xsl:text>: </xsl:text>
        <xsl:value-of select="ID" />
        <xsl:text> </xsl:text>
        <xsl:apply-templates/>
    </xsl:template>   

    <xsl:template match="text()"/>

</xsl:transform>

Why this is better: It will more properly control the text output (assuming you really do want to output this to plaintext format). It also is more XSLT template oriented, which tends to be more extensible and maintainable than having for-each loops hanging around.

Using the descendant-or-self axis:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="//Managers">
       <xsl:for-each select="descendant-or-self::Manager">
        <xsl:value-of select="Name" />: <xsl:value-of select="ID" /><xsl:text> </xsl:text>
      </xsl:for-each>
    </xsl:template>    

</xsl:transform>

Output:

  Matt: 1 Joe: 11 Dave: 111 mike: 2 

http://xsltransform.net/nc4NzQB

Sign up to request clarification or add additional context in comments.

2 Comments

If you output a space character within xsl:text, then why not put : inside xsl:text, too? Also, I don't think there is a need to match Managers, xsltransform.net/nc4NzQB/2.
I did the space character because otherwise it gets absorbed - but the : doesn't. Your method is better though, I didn't think to use apply-templates within the template.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.