我有字符串作为"Aircraft Crash" "Aircraft Hijacking" Avalanche Flood
我可以将其输出为:
<item>Aircraft Crash</item>
<item>Aircraft Hijacking</item>
<item>Avalanche</item>
<item>Flood</item>示例:https://gist.github.com/netsi1964/2648824
但是如何使用XSLT1.0生成["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>至
<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>XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="xxxxxxxxxxxx">
<xsl:output method="xml" indent="yes"/>
<!-- ****************************************************************** -->
<xsl:template match="/">
<xsl:apply-templates select="s3xml"/>
</xsl:template>
<!-- ****************************************************************** -->
<xsl:template match="/s3xml">
<xsl:apply-templates select="./resource[@name='cap_alert']"/>
</xsl:template>
<!-- ****************************************************************** -->
<xsl:template match="resource[@name='cap_alert']">
<alert>
<identifier>
<xsl:value-of select="data[@field='identifier']"/>
</identifier>
<sender>
<xsl:value-of select="data[@field='sender']"/>
</sender>
<status>
<xsl:value-of select="translate(data[@field='status']/@value, '"', '')"/>
</status>
<incidents>??????</incidents>
</alert>
</xsl:template>
</xsl:stylesheet>发布于 2016-06-21 15:48:39
这段代码将创建一个包含所需内容的变量,您仍然需要将其放在正确的位置:
<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>发布于 2016-06-22 16:44:40
下面的模板将把以空格分隔的字符串拆分为一组节点
<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处理程序需要将碎片转换为可工作的节点集。有关如何做到这一点的示例,请参阅http://www.xml.com/pub/a/2003/07/16/nodeset.html。
https://stackoverflow.com/questions/37938253
复制相似问题