Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.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
Xslt 如果存在子节点,则创建新的XML节点_Xslt - Fatal编程技术网

Xslt 如果存在子节点,则创建新的XML节点

Xslt 如果存在子节点,则创建新的XML节点,xslt,Xslt,我一直在尝试使用XSLT实现以下输出,但一直在努力。提前感谢您的帮助 从 第一行 第二行 案文的另一段 第三段但不中断 到 第一行 第二行 案文的另一段 第三段但不中断 谢谢, Dono假设您的元素只包含文本和元素,并且您希望规范化空白并排除仅包含空白的元素(由您所需的输出建议),以下操作应能起作用: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <

我一直在尝试使用XSLT实现以下输出,但一直在努力。提前感谢您的帮助


第一行
第二行
案文的另一段
第三段但不中断


第一行
第二行
案文的另一段
第三段但不中断
谢谢,

Dono

假设您的
元素只包含文本和
元素,并且您希望规范化空白并排除仅包含空白的
元素(由您所需的输出建议),以下操作应能起作用:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes"/>

    <xsl:template match="par">
        <document>
            <xsl:apply-templates select="*"/>
        </document>
    </xsl:template>

    <xsl:template match="run">
        <xsl:for-each select="text()">
            <xsl:if test="normalize-space(self::text()) != ''">
                <para>
                    <xsl:value-of select="normalize-space(self::text())"/>
                 </para>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>


这里有一个简单的解决方案,它是面向推送的,不需要
self::

当此XSLT:

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

  <xsl:template match="/*">
     <document>
       <xsl:apply-templates />  
     </document>
  </xsl:template>

  <xsl:template match="run/text()">
     <para>
       <xsl:value-of select="normalize-space()" />
     </para>
  </xsl:template>

</xsl:stylesheet>
<par>
   <run>Line one<break/>
        Line two<break/>
   </run>

   <run>Another para of text<break/>
   </run>

   <run>3rd para but no break</run>    
</par>
<document>
  <para>Line one</para>
  <para>Line two</para>
  <para>Another para of text</para>
  <para>3rd para but no break</para>
</document>

…应用于提供的XML:

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

  <xsl:template match="/*">
     <document>
       <xsl:apply-templates />  
     </document>
  </xsl:template>

  <xsl:template match="run/text()">
     <para>
       <xsl:value-of select="normalize-space()" />
     </para>
  </xsl:template>

</xsl:stylesheet>
<par>
   <run>Line one<break/>
        Line two<break/>
   </run>

   <run>Another para of text<break/>
   </run>

   <run>3rd para but no break</run>    
</par>
<document>
  <para>Line one</para>
  <para>Line two</para>
  <para>Another para of text</para>
  <para>3rd para but no break</para>
</document>

第一行
第二行
案文的另一段
第三段但不中断
…生成所需结果:

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

  <xsl:template match="/*">
     <document>
       <xsl:apply-templates />  
     </document>
  </xsl:template>

  <xsl:template match="run/text()">
     <para>
       <xsl:value-of select="normalize-space()" />
     </para>
  </xsl:template>

</xsl:stylesheet>
<par>
   <run>Line one<break/>
        Line two<break/>
   </run>

   <run>Another para of text<break/>
   </run>

   <run>3rd para but no break</run>    
</par>
<document>
  <para>Line one</para>
  <para>Line two</para>
  <para>Another para of text</para>
  <para>3rd para but no break</para>
</document>

第一行
第二行
案文的另一段
第三段但不中断

请包含一些要显示的代码