0

I am very new to XML,XSLT. I need to create a presentation template using XSLT for my data capture template to output it as a json. i.e., convert a XML file to JSON using XSLT.

I'm using the following XSLT to convert XML to JSON: https://github.com/bojanbjelic/xml2json/blob/master/xml2json.xsl

It's working great but I would really like to extend it so that the text in the "OfferContent" field escapes the double quotes.

This is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE data-capture-requirements SYSTEM "datacapture6.0.dtd">
<data-capture-requirements name="test" type="content">
    <!-- data-capture-requirements elements contain area elements -->
    <ruleset name="WebOffersDCT">
    <script src="/iw/vanguard/CaaS/CaaSValidation.js" location="webserver" language="javascript"/>
    <script><![CDATA[IWEventRegistry.addFormHandler("onFormInit", validatePageID);]]></script> 
       <description> Offers DCT</description>
          <root-container location="Offers_Config" name="Offers_Config">
            <container location="content" name="content">
                <label>content</label>
                <item name="ContentID" pathid="ContentID" required="t"> 
                    <label>Content ID</label>
                    <description></description>
                    <text maxlength="200" required="t" size="90"/>
                </item>
                <item name="LandingSpot" pathid="landingSpot">
                    <label>Landing Spot</label>
                    <description/>
                    <text maxlength="200" required="t" size="90"/>
                </item>
                <item name="OfferContent" pathid="offerContent">
                    <label>Offer Content</label>
                    <description/>
                    <textarea cols="75" external-editor="tinymce"
                        external-editor-config="migrateVFEMCE"
                        external-editor-inline="t" rows="20" wrap="virtual"/>
                </item>
                <item name="ContentLink" pathid="ContentLink">
                    <label>Content Link</label>
                    <description/>
                    <text maxlength="200" required="f" size="90"/>
                </item>
                <item name="Description" pathid="Description">
                    <label>Description</label>
                    <description/>
                    <text maxlength="200" required="f" size="90"/>
                </item>
                <item name="Title" pathid="title">
                    <label>Title</label>
                    <description/>
                    <text maxlength="200" required="f" size="90"/>
                </item>   
              <item name="KeyPoints" pathid="keypoints">
                    <label>KeyPoints</label>
                    <description/>
                    <textarea cols="75" external-editor="tinymce"
                        external-editor-config="migrateVFEMCE"
                        external-editor-inline="t" rows="20" wrap="virtual"/>
                </item>
                <item name="Rationale" pathid="Rationale">
                    <label>Rationale</label>
                    <description/>
                    <text maxlength="200" required="f" size="90"/>
                </item>
                <item name="Theme" pathid="Theme">
                    <label>Theme</label>
                    <description/>
                    <text maxlength="200" required="f" size="90"/>
                </item>
           </container>
       </root-container>
    </ruleset>
</data-capture-requirements>

and this is my current output using a sample Data Capture Record:

{"Offers_Config" : {"content" : {"ContentID" : "PC_1534", "landingSpot" : "LG_LOGON", "offerContent" : "<!--PPE: OfferId:PC_1475;LandingSpot:LG_LOGON --><a class="noborder" href="cbd:topicurl"><img style="border: none;" title="Vote in Vanguard's 2017 proxy campaign. We need your vote on the election of trustees as well as on several fund-related proposals that will allow us to serve our clients' needs most effectively. Every vote counts and it only takes a few minutes. Vote Now. Shared by VMC on behalf of VGI." src="/web/images/PC_1475_LO.png" alt="Vote in Vanguard's 2017 proxy campaign. We need your vote on the election of trustees as well as on several fund-related proposals that will allow us to serve our clients' needs most effectively. Every vote counts and it only takes a few minutes. Vote Now. Shared by VMC on behalf of VGI." /></a><!--End PPE-->", "ContentLink" : "https://investor.vanguard.com/529-plan/open-account?cmpgn=WO:RIG:AQ:VG529Pro:seclogo:LOGOFF:1118:0101:529:529:0323", "Description" : "", "title" : "MobyWO", "keypoints" : "", "Rationale" : "Client communication", "Theme" : "Proxy"}}}

My expected output is:

{"Offers_Config" : {"content" : {"ContentID" : "PC_1534", "landingSpot" : "LG_LOGON", "offerContent" : "<!--PPE: OfferId:PC_1475;LandingSpot:LG_LOGON --><a class=\"noborder\" href=\"cbd:topicurl\"><img style=\"border: none;\" title=\"Vote in Vanguard's 2017 proxy campaign. We need your vote on the election of trustees as well as on several fund-related proposals that will allow us to serve our clients' needs most effectively. Every vote counts and it only takes a few minutes. Vote Now. Shared by VMC on behalf of VGI.\" src=\"/web/images/PC_1475_LO.png\" alt=\"Vote in Vanguard's 2017 proxy campaign. We need your vote on the election of trustees as well as on several fund-related proposals that will allow us to serve our clients' needs most effectively. Every vote counts and it only takes a few minutes. Vote Now. Shared by VMC on behalf of VGI.\" /><\/a><!--End PPE-->", "ContentLink" : "https://investor.vanguard.com/529-plan/open-account?cmpgn=WO:RIG:AQ:VG529Pro:seclogo:LOGOFF:1118:0101:529:529:0323", "Description" : "", "title" : "MobyWO", "keypoints" : "", "Rationale" : "Client communication", "Theme" : "Proxy"}}}

There's some good code here: XSLT + Replacing double quotes with escape sequence.

<xsl:template name="escapeQuote">
  <xsl:param name="pText" select="."/>
  <xsl:if test="string-length($pText) >0">
   <xsl:value-of select="substring-before(concat($pText, '&quot;'), '&quot;')"/>
   <xsl:if test="contains($pText, '&quot;')">
    <xsl:text>\"</xsl:text>
    <xsl:call-template name="escapeQuote">
      <xsl:with-param name="pText" select="substring-after($pText, '&quot;')"/>
    </xsl:call-template>
   </xsl:if>
  </xsl:if>
</xsl:template>

But unfortunately I'm not experienced enough with XSLTs to integrate it with what the code I'm using from above. Any help would be much appreciated. Thanks in advance!

nvps
  • 13
  • 3
  • 1
    With XSLT questions, please always tell us which XSLT version you are using. Most problems are much easier to solve using XSLT 2.0 or 3.0, but many users are still using the 20-year old XSLT 1.0. – Michael Kay Jun 17 '20 at 08:20
  • Hi @MichaelKay ! Thank you for your response. The code still uses 1.0 version. – nvps Jun 17 '20 at 08:37

0 Answers0