输入XML
<Address country="USA" countryISO2Code="US">
<Street addr="GH Nagar"/>
<Street addr="Naboor"/>
<Street addr="Besides Al Drive Inn"/>
</Address>
预期输出回复
"address": {
"country": "USA",
"countryISO2Code": "US",
"Street1": "GH Nagar" ,
"Street2": "Naboor" ,
"Street3": "Besides Al Drive Inn"
}
任何人都可以建议为上述内容编写xsl。
答案 0 :(得分:0)
您可以使用:
<xsl:output indent="yes" method="text"></xsl:output>
<xsl:template match="Address">
<xsl:text>"address":{</xsl:text>
<xsl:for-each select="@*">
<xsl:value-of select="concat('
"', local-name(.), '": "', ., '",')"/>
</xsl:for-each>
<xsl:for-each select="Street">
<xsl:text>
</xsl:text>
<xsl:value-of select="concat('"',local-name(.), position(), '": "',@addr,'",')"/>
</xsl:for-each>
<xsl:text>
}</xsl:text>
</xsl:template>
<强>输出强>
"address":{
"country": "USA",
"countryISO2Code": "US",
"Street1": "GH Nagar",
"Street2": "Naboor",
"Street3": "Besides Al Drive Inn",
}
答案 1 :(得分:-1)
它没有按照你的建议工作。你可以查看下面的内容。除了&#34; Street&#34;我能够获得其他值。一个&#39; S
要求您仅针对街道部分建议更改。
我的xsl是
<xsl:for-each select="./Address/Street">
<xsl:text></xsl:text>
<xsl:value-of select="concat('"',local-name(.), position(), '": "',@addr,'",')" />
</xsl:for-each>
输出是
#text": "\"Street1\": \"address1\",\"Street2\": \"address2\",\"Street3\": \"address3\",",
预期产出
&#34; Street1&#34;:&#34; GH Nagar&#34; , &#34; Street2&#34;:&#34; Naboor&#34; , &#34; Street3&#34;:&#34;除了Al Drive Inn&#34;