将XML转换为JSON,使用XSLT 2.0。

huangapple go评论72阅读模式
英文:

Convert XML to JSON with XSLT 2.0

问题

你想要将名为"id"的XML元素的非空值转换为字符串,你可以尝试以下修改:

<xsl:template match="id" mode="json">
    <xsl:choose>
        <xsl:when test=".">
            <xsl:text>"<xsl:value-of select="."/>"</xsl:text>
        </xsl:when>
    </xsl:choose>
</xsl:template>

这将检查"id"元素是否存在并且非空,如果是,则将其值转换为字符串格式。

希望这对你有所帮助。如果你有其他问题,请随时告诉我。

英文:

I have a XSLT that can convert XML to JSON and it works well. But When I'm trying to modify the value type of JSON I'm unable to do so.

Below is my XSLT file code:

&lt;xsl:template match=&quot;json:name&quot; mode=&quot;json&quot;&gt;
    &lt;xsl:text/&gt;&quot;&lt;xsl:value-of select=&quot;json:encode-string(.)&quot;/&gt;&quot;:&lt;xsl:text/&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;json:value&quot; mode=&quot;json&quot;&gt;
    &lt;xsl:choose&gt;
        &lt;xsl:when test=&quot;node() and not(text())&quot;&gt;
            &lt;xsl:apply-templates mode=&quot;json&quot;/&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:when test=&quot;text()&quot;&gt;
            &lt;xsl:choose&gt;
                                    &lt;xsl:when test=&quot;normalize-space(.) ne . or not((string(.) castable as xs:integer and not(starts-with(string(.),&#39;+&#39;) or starts-with(string(.),&#39;-&#39;)) and not(starts-with(string(.),&#39;0&#39;) and not(. = &#39;0&#39;))) or (string(.) castable as xs:decimal and not(starts-with(string(.),&#39;+&#39;)) and not(starts-with(.,&#39;-.&#39;)) and not(starts-with(.,&#39;.&#39;)) and not(starts-with(.,&#39;-0&#39;) and not(starts-with(.,&#39;-0.&#39;))) and not(ends-with(.,&#39;.&#39;)) and not(starts-with(.,&#39;0&#39;) and not(starts-with(.,&#39;0.&#39;))))) and not(. = &#39;false&#39;) and not(. = &#39;true&#39;) and not(. = &#39;null&#39;)&quot;&gt;
                                        &lt;xsl:text/&gt;&quot;&lt;xsl:value-of select=&quot;json:encode-string(.)&quot;/&gt;&quot;&lt;xsl:text/&gt;
                                    &lt;/xsl:when&gt;
                &lt;xsl:otherwise&gt;
                    &lt;xsl:text/&gt;&lt;xsl:value-of select=&quot;.&quot;/&gt;&lt;xsl:text/&gt;
                &lt;/xsl:otherwise&gt;

            &lt;/xsl:choose&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
            &lt;xsl:text&gt;null&lt;/xsl:text&gt;
        &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
&lt;/xsl:template&gt;

I want to add a condition that if xml element name is id and it's non-null, then convert it to string.

I tried below thing, but it didn't work.

    &lt;xsl:template match=&quot;id&quot;&gt;
    &lt;xsl:choose&gt;
        &lt;xsl:when test=&quot;id&quot;&gt;
            &lt;xsl:text/&gt;&quot;&lt;xsl:value-of select=&quot;json:encode-string(.)&quot;/&gt;&quot;&lt;xsl:text/&gt;
        &lt;/xsl:when&gt;
    &lt;/xsl:choose&gt;
&lt;/xsl:template&gt;

How do I iterate over these elements as key-value pair, so I can add conditional logic to value if name matches?

Edit:

Added sample XML:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
 &lt;ns0:TestMe xmlns:ns0=&quot;urn:hero:wakanda:Cle:Something&quot;&gt;
   &lt;root&gt;
     &lt;id&gt;123&lt;/id&gt;
     &lt;field2&gt;345&lt;/field2&gt;
     &lt;fee&gt;
        &lt;totalFee&gt;123&lt;/totalFee&gt;
    &lt;/fee&gt;
    &lt;/root&gt;
    &lt;/ns0:TestMe&gt;

Using this XML to JSON converter file: https://github.com/bramstein/xsltjson/blob/master/conf/xml-to-json.xsl

答案1

得分: 1

在与您的问题编辑链接的样式表上下文中,我认为您想要类似以下的内容:

<xsl:template match="json:member[json:name = 'id']/json:value" mode="json">
  <xsl:text>"</xsl:text>
  <xsl:value-of select="json:encode-string(.)"/>
  <xsl:text>"</xsl:text>
</xsl:template>

以获取例如 &quot;id&quot;:&quot;123&quot; 而不是例如 &quot;id&quot;:123

英文:

In the context of the stylesheet linked to from your question edit I think you want something like

  &lt;xsl:template match=&quot;json:member[json:name = &#39;id&#39;]/json:value&quot; mode=&quot;json&quot;&gt;
    &lt;xsl:text&gt;&quot;&lt;/xsl:text&gt;
    &lt;xsl:value-of select=&quot;json:encode-string(.)&quot;/&gt;
    &lt;xsl:text&gt;&quot;&lt;/xsl:text&gt;
  &lt;/xsl:template&gt;

to get e.g. &quot;id&quot;:&quot;123&quot; instead of e.g. &quot;id&quot;:123.

huangapple
  • 本文由 发表于 2023年5月24日 20:16:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定