Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在xsl中拆分包含内容的字符串/_Xml_String_Xslt - Fatal编程技术网

Xml 在xsl中拆分包含内容的字符串/

Xml 在xsl中拆分包含内容的字符串/,xml,string,xslt,Xml,String,Xslt,我使用xsl从外部xml中提取了一些内容。在xml中,标题与作者合并,并用反斜杠分隔 如何在xsl中分离标题和作者,以便使用不同的标记 <product> <title>The Maze / Jane Evans</title> </product> 迷宫/简·埃文斯 将来 <h2>The Maze</h2> <p>Jane Evans</p> 迷宫 简·埃文斯 希望这有帮助!如果我误解

我使用xsl从外部xml中提取了一些内容。在xml中,标题与作者合并,并用反斜杠分隔

如何在xsl中分离标题和作者,以便使用不同的标记

<product>
  <title>The Maze / Jane Evans</title> 
</product>

迷宫/简·埃文斯
将来

<h2>The Maze</h2>
<p>Jane Evans</p>
迷宫
简·埃文斯


希望这有帮助!如果我误解了这个问题,请告诉我

<xsl:variable name="title">
    <xsl:value-of select="/product/title"/>
</xsl:variable>

<xsl:template match="/">
    <xsl:choose>
        <!--create new elements from existing text-->
        <xsl:when test="contains($title, '/')">
            <xsl:element name="h2">
                <xsl:value-of select="substring-before($title, '/')"/>
            </xsl:element>
            <xsl:element name="p">
                <xsl:value-of select="substring-after($title, '/')"/>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
            <!--no '/' deliminator exists-->
            <xsl:value-of select="$title"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

此转换:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="title[contains(., '/')]">
   <h2>
    <xsl:value-of select="substring-before(., '/')"/>
   </h2>
   <p>
    <xsl:value-of select="substring-after(., '/')"/>
   </p>
 </xsl:template>

 <xsl:template match="title">
   <h2><xsl:value-of select="."/></h2>
 </xsl:template>
</xsl:stylesheet>
<product>
  <title>The Maze / Jane Evans</title>
</product>
<h2>The Maze </h2>
<p> Jane Evans</p>


应用于提供的XML文档时

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="title[contains(., '/')]">
   <h2>
    <xsl:value-of select="substring-before(., '/')"/>
   </h2>
   <p>
    <xsl:value-of select="substring-after(., '/')"/>
   </p>
 </xsl:template>

 <xsl:template match="title">
   <h2><xsl:value-of select="."/></h2>
 </xsl:template>
</xsl:stylesheet>
<product>
  <title>The Maze / Jane Evans</title>
</product>
<h2>The Maze </h2>
<p> Jane Evans</p>

迷宫/简·埃文斯
产生想要的结果

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="title[contains(., '/')]">
   <h2>
    <xsl:value-of select="substring-before(., '/')"/>
   </h2>
   <p>
    <xsl:value-of select="substring-after(., '/')"/>
   </p>
 </xsl:template>

 <xsl:template match="title">
   <h2><xsl:value-of select="."/></h2>
 </xsl:template>
</xsl:stylesheet>
<product>
  <title>The Maze / Jane Evans</title>
</product>
<h2>The Maze </h2>
<p> Jane Evans</p>
迷宫
简·埃文斯

请注意,没有使用显式的条件代码——XSLT处理器自己完成这项工作。

我已经编写了一个这样做的程序


好问题(+1)。请参阅我的答案,以获得完全符合XSLT精神的简短而简单的解决方案。谢谢!我很高兴这有帮助。如果你觉得这是正确的解决方案。。我不介意我的第一个被接受的解决方案;)