1

I have an XSL Code as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />

  <xsl:variable name="inlineAddressArray">
    <addrline>Home1</addrline>
    <addrline>Home2</addrline>
    <addrline>Home3</addrline>
  </xsl:variable>

  <xsl:variable xmlns:exsl="http://exslt.org/common" name="data" select="exsl:node-set($inlineAddreddArray)" />

  <addressLines>
    <xsl:value-of select="$data/addrline" />
  </addressLines>

</xsl:stylesheet>

My expected JSON output should be:

"addressLines":"[Home1,Home2,Home3]"

Output that I'm getting

"addressLines": "Home1Home2Home3"

Basically it is just concatenating every element as a single element.

But, I should get three elements separately as shown above.

Can anyone please help me on this? Don't mind if it's a silly query. I'm very new to XSLT :)

5
  • Why is your question tagged xslt-2.0 when you are obviously using (or rather misusing) XSLT 1.0? And what is the point of defining a variable with hard-coded elements, when you need a string? Commented Apr 15, 2022 at 9:16
  • @michael.hor257k- Yes, I'm using XSLT 2.0. And the values present inside my request are string type. For easy understanding I've hardcoded in the question. Commented Apr 15, 2022 at 9:25
  • There is no need to use exsl:node-set() in XSLT 2.0. And your output is XML, not JSON, so it's not clear how JSON comes into this at all. Commented Apr 15, 2022 at 9:41
  • It is not clear what you want to achieve, the "expected JSON" "addressLines":"[Home1,Home2,Home3]" doesn't look like JSON. Also, your XSLT outputs XML, do you integrate XSLT in some other tool chain where that XML is being converted to JSON? In that case you need to name the tool chain. Commented Apr 15, 2022 at 11:57
  • @MartinHonnen - yes that's correct. I'm converting XML to JSON via Newtonsoft. I'm using Newtonsoft.Json.JsonConvert.SerializeXmlNode() function to convert Commented Apr 20, 2022 at 15:16

1 Answer 1

1

No need for Newtonsoft.Json.JsonConvert.SerializeXmlNode() .

Given this xml:

<inlineAddressArray>
  <addrline>Home1</addrline>
  <addrline>Home2</addrline>
  <addrline>Home3</addrline>
</inlineAddressArray>

Using this xslt 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="text"  encoding="UTF-8" />
  
  <xsl:template match="inlineAddressArray">
    <xsl:text>"addressLines":"[</xsl:text>
    <xsl:value-of select="addrline" separator=","/>
    <xsl:text>]"</xsl:text>
  </xsl:template>
  
</xsl:stylesheet>

or using xslt 1.0

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"  encoding="UTF-8" />
  
  <xsl:template match="inlineAddressArray">
    <xsl:text>"addressLines":"[</xsl:text>
    <xsl:apply-templates select="addrline"/>
    <xsl:text>]"</xsl:text>
  </xsl:template>
  
  <xsl:template match="addrline">
    <xsl:value-of select="."/>
    <xsl:if test="not(position()=last())">
      <xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

results in this :

"addressLines":"[Home1,Home2,Home3]"

UPDATE

if you also want to deal with empty addrline like i.e.

<inlineAddressArray>
  <addrline>Home1</addrline>
  <addrline>Home2</addrline>
  <addrline></addrline>
</inlineAddressArray>

Your xslt 1.0 could look like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"  encoding="UTF-8" />
  
  <xsl:template match="inlineAddressArray">
    <xsl:text>"addressLines":"[</xsl:text>
    <xsl:apply-templates select="addrline"/>
    <xsl:text>]"</xsl:text>
  </xsl:template>
  
  <xsl:template match="addrline">
    <xsl:value-of select="."/>
    <xsl:if test=" following-sibling::addrline[(normalize-space(.))]">
      <xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="addrline[not(normalize-space(.))]"/>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, Thank you for your answer. I'm getting separator attribute is not declared issue. Could you help me to resolve this issue.
Are you sure your processor support xslt 2.0?
Thank you very much for your answer. It works like a charm using XSLT 1.0 code. But I have a scenario where if one of the elements doesn't have any value, it shouldn't be populating an empty string. For example let's say I have <inlineAddressArray> <addrline>Home1</addrline> <addrline>Home2</addrline> <addrline></addrline> </inlineAddressArray> My output should be ["Home1","Home2"] Could you please tell me how can I make this work?

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.