我的字符串为UINavigationBar
我可以将其输出为:
"Aircraft Crash" "Aircraft Hijacking" Avalanche Flood
示例:https://gist.github.com/netsi1964/2648824
但是如何使用XSLT 1.0生成<item>Aircraft Crash</item>
<item>Aircraft Hijacking</item>
<item>Avalanche</item>
<item>Flood</item>
?
["Aircraft Crash", "Aircraft Hijacking", "Avalanche", "Flood"]
到
<alert xmlns="xxxxxxxxxxxx">
<identifier>203.81.87.42--20160621-583-</identifier>
<sender>12.12.4</sender>
<sent>2016-06-21T05:17:02+00:00</sent>
<status>Test</status>
<incidents>
"Aircraft Crash" "Aircraft Hijacking" Avalanche Flood
</incidents>
</alert>
XSLT:
<s3xml success="true">
<resource name="cap_alert" uuid="urn:uuid:b5305e2b-9aa6-45ae-bb34-ed777cedbc3a" created_by="admin@example.com">
<data field="identifier">203.81.87.42--20160621-583-</data>
<data field="incidents" value="["Aircraft Crash", "Aircraft Hijacking", "Avalanche", "Flood"]">
Aircraft Crash, Aircraft Hijacking, Avalanche, Flood
</data>
<data field="sender">12.12.4</data>
<data field="sent" value="2016-06-21T05:17:02">June 21, 2016 12:17:02</data>
<data field="status" value=""Test"">Test - testing, all recipients disregard</data>
</resource>
</s3xml>
答案 0 :(得分:0)
此代码段会创建一个包含所需内容的变量,您仍需将其放在正确的位置:
<xsl:variable name="Array">
<xsl:text>[</xsl:text>
<xsl:for-each select="item">
<xsl:text>"</xsl:text>
<xsl:value-of select="." />
<xsl:text>"</xsl:text>
<xsl:if test="position() != last()"><xsl:text>, </xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:variable>
答案 1 :(得分:0)
以下模板会将空格分隔的字符串拆分为一组&lt; value /&gt;节点
<xsl:template name="split">
<xsl:param name="string"/>
<xsl:variable name="testString" select="normalize-space($string)"/>
<xsl:choose>
<xsl:when test="$testString=''"/>
<xsl:when test="contains($testString,'"') and substring-before($testString,'"')=''">
<xsl:variable name="afterQuote" select="substring-after($testString,'"')"/>
<xsl:variable name="quotedString" select="substring-before($afterQuote,'"')"/>
<value><xsl:value-of select="$quotedString"/></value>
<xsl:call-template name="split">
<xsl:with-param name="string" select="substring-after($afterQuote,'"')"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="substring-before($testString,' ')">
<value><xsl:value-of select="substring-before($testString,' ')"/></value>
<xsl:call-template name="split">
<xsl:with-param name="string" select="substring-after($testString,' ')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<value><xsl:value-of select="$testString"/></value>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
现在将值作为节点集,您可以使用它来生成所需的任何输出。在你的情况下,你将使用这样的东西......
<xsl:template match="incidents">
<xsl:variable name="incidentset">
<xsl:call-template name="split">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</xsl:variable>
<!-- at this point $incidentset will be a fragment:
<value>Aircraft Crash</value>
<value>Aircraft Hijacking</value>
<value>Avalanche</value>
<value>Flood</value>
-->
<data field="incidents" >
<xsl:attribute name="value">
<xsl:text>[</xsl:text>
<!-- many xsl processors required $incidentset to be converted into node-set, mine doesn't!-->
<xsl:for-each select="$incidentset/*">
<xsl:value-of select="concat('"',text(),'"')" disable-output-escaping="no"/>
<xsl:if test="position()!=last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:attribute>
<!-- many xsl processors required $incidentset to be converted into node-set, mine doesn't!-->
<xsl:for-each select="$incidentset/*">
<xsl:value-of select="text()"/>
<xsl:if test="position()!=last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
</data>
</xsl:template>
请注意,某些xsl处理器将要求您将fragement转换为可行的节点集。有关如何执行此操作的示例,请参阅http://www.xml.com/pub/a/2003/07/16/nodeset.html。