От XML до JSON с использованием XSLT и экранирования двойных кавычек в тексте - PullRequest
0 голосов
/ 17 июня 2020

Я новичок в XML, XSLT. Мне нужно создать шаблон презентации с использованием XSLT для моего шаблона сбора данных, чтобы вывести его как json. т.е. преобразовать файл XML в JSON с помощью XSLT.

Я использую следующий XSLT для преобразования XML в JSON: https://github.com/bojanbjelic/xml2json/blob/master/xml2json.xsl

Отлично работает, но мне бы очень хотелось расширить его, чтобы текст в поле «OfferContent» избегал двойных кавычек.

Это мой 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>

и это мой текущий результат с использованием образца записи захвата данных:

{"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"}}}

Мой ожидаемый результат это:

{"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"}}}

Здесь есть хороший код: XSLT + Замена двойных кавычек на escape-последовательность.

<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>

Но, к сожалению, у меня недостаточно опыта работы с XSLT, чтобы интегрировать его с тем кодом, который я использую сверху. Любая помощь приветствуется. Заранее спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...