【问题标题】:xslt transforming xml to xml by carring text with node in XSLTxslt 通过在 XSLT 中携带带有节点的文本将 xml 转换为 xml
【发布时间】:2015-05-29 06:55:51
【问题描述】:

我想将一个 XML 文件转换为 xml,但我还想将一些文本与其他元素进行转换

输入:

<parent> Some parent text
 <child1>child text</child1><child2>child text</child2>
   more parent text</parent>

期望的输出:

<parent><text>Some parent text</text>
 <child1>child text</child><child2>child text</child2>
   <text>more parent text</text></parent>

我的 XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="text" indent="no"/>
<xsl:template match="node()">
    <xsl:copy>
       <xsl:apply-templates select="node()"/>
    </xsl:copy>
  </xsl:template>
<xsl:template match="parent/text()">
     <xsl:element name="node">
         <xsl:apply-templates select="text()"/>
     </xsl:element>
  </xsl:template>
<xsl:template match="child1">
     <xsl:element name="child1">
         <xsl:apply-templates select="node()"/>
     </xsl:element>
  </xsl:template>
<xsl:template match="child2">
     <xsl:element name="child2">
         <xsl:apply-templates select="node()"/>
     </xsl:element>
  </xsl:template>
</xsl:stylesheet>

无论如何我可以解决这个问题以包含一些带有子节点的子文本吗?

【问题讨论】:

    标签: xslt


    【解决方案1】:

    首先需要设置输出方式为xml:

    <xsl:output method="xml" indent="no"/>
    

    然后通过修改模板处理文本:

    <xsl:template match="parent/text()">
         <xsl:element name="text">
             <xsl:value-of select="." />
         </xsl:element>
    </xsl:template>
    

    这是我的结果输出:

    <?xml version="1.0" encoding="UTF-8"?>
    <parent>
       <text> Some parent text
     </text>
       <child1>child text</child1>
       <child2>child text</child2>
       <text>
       more parent text</text>
    </parent>
    

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 2019-01-23
      • 2019-08-03
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      相关资源
      最近更新 更多