0

我对 XML、XSLT 很陌生。我需要使用 XSLT 为我的数据捕获模板创建一个演示模板,以将其输出为 json。即,使用 XSLT 将 XML 文件转换为 JSON。

我正在使用以下 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 + 用转义序列替换双引号。

<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 的经验不足,无法将它与我上面使用的代码集成。任何帮助将非常感激。提前致谢!

4

0 回答 0