2016-06-21 71 views
0

我的字符串作为"Aircraft Crash" "Aircraft Hijacking" Avalanche FloodXSLT - 斯普利特报价到列表

我可以输出为:

<item>Aircraft Crash</item> 
<item>Aircraft Hijacking</item> 
<item>Avalanche</item> 
<item>Flood</item> 

例如:https://gist.github.com/netsi1964/2648824

但使用XSLT 1.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="[email protected]"> 
     <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, '&quot;', '')"/> 
     </status> 
     <incidents>??????</incidents> 
    </alert> 
</xsl:template> 
</xsl:stylesheet> 
+0

你的意思是说你希望重新使用github中的xsl,以便它处理你的初始空格分隔的字符串以产生json数组输出? –

+0

什么是您的执行环境 - 您是否甚至需要使用xsl进行如此简单的字符串转换? –

+0

是的,我正在为此用例使用XSL。字符串通常采用上述格式 - 如果单引号分隔,则以空格分隔。 '输入:这是“一个字符串”。 输出:[“This”,“is”,“a string”] 输入:“飞机坠毁”“飞机劫持”雪崩洪水 输出:“飞机坠毁”“劫机劫机”“劫持飞机”洪水“]' –

回答

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> 
+0

这给了我“飞机坠毁”“飞机劫持”雪崩洪水 通知没有逗号 –

+0

不知道...当我检查它时不是。无论如何,我认为每个人都把整个字符串视为单个,所以基本上我得到了不同意的结果! –

+0

你正在使用哪种编译器?你可以发布XSLT吗? –

0

以下模板将会拆分与价值观为节点集,你可以使用它来制作自己需要的任何输出你的空间,delimeted字符串转换成一组<值/>节点

<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,'&quot;') and substring-before($testString,'&quot;')=''"> 
       <xsl:variable name="afterQuote" select="substring-after($testString,'&quot;')"/> 
       <xsl:variable name="quotedString" select="substring-before($afterQuote,'&quot;')"/> 
       <value><xsl:value-of select="$quotedString"/></value> 
       <xsl:call-template name="split"> 
        <xsl:with-param name="string" select="substring-after($afterQuote,'&quot;')"/> 
       </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('&quot;',text(),'&quot;')" 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