【问题标题】:Copy all node exclude modified node复制所有节点排除修改节点
【发布时间】:2014-02-14 19:12:37
【问题描述】:

我有一个如下的xml:

<root>
  <book>abcd</book>
  <pen>ghi</pen>
  <source Qual="poor" vise="n">

    <source>
      <input value="in"></input>
      <input></input>
    </source>

  </source>
  <class>ab</class>
  <source>noaatributes</source>
  <studio>ghi</studio>
  <source Qual="good" vise="m">

    <source>
      <input></input>
      <input value="out"></input>
    </source>

  </source>
</root>

我想将xml转换成以下格式:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <book>abcd</book>
  <pen>ghi</pen>
  <poor value="in"/>


  <class>ab</class>
  <source>noaatributes</source>
  <studio>ghi</studio>
  <good value="out"/>



</root>

但是我编写的代码没有提供所需的输出..我卡在了我的编码中间..请提供任何建议。 这是我到目前为止编写的代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">


      <xsl:apply-templates select="@* | node()" />



  </xsl:template>


  <xsl:template match="source/@Qual">
    <xsl:text disable-output-escaping="yes" ><![CDATA[  
    <]]></xsl:text>
    <xsl:value-of disable-output-escaping="yes" select="."/>
  </xsl:template>

  <xsl:template match="source/source">
    <xsl:apply-templates select="input"/>
    <xsl:text disable-output-escaping="yes" ><![CDATA[  
    />]]></xsl:text>
  </xsl:template>

  <xsl:template match="input">
    <xsl:if test ="string-length(./@value) &gt; 0">
      <xsl:text disable-output-escaping="yes" ><![CDATA[    
      ]]></xsl:text>
      <xsl:text disable-output-escaping="yes" ><![CDATA[Value="]]></xsl:text>
      <xsl:value-of disable-output-escaping="yes" select="./@value"/>
      <xsl:text disable-output-escaping="yes" ><![CDATA[" ]]></xsl:text>
    </xsl:if>

  </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt xpath xml-parsing xslt-1.0


    【解决方案1】:

    这个会小一点

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="root/source[@Qual]">
        <xsl:element name="{@Qual[1]}">
            <xsl:copy-of select="source/input/@value[1]"/>
        </xsl:element>
    </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      【解决方案2】:

      要获得你想要的输出,可以简单得多:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
          <xsl:output method="xml" indent="yes"/>
      
          <xsl:template match="root">
              <xsl:copy>
                  <xsl:apply-templates/>
              </xsl:copy>
          </xsl:template>
      
          <xsl:template match="*[not(source[@Qual])]">
              <xsl:copy-of select="."/>
          </xsl:template>
      
          <xsl:template match="source[@Qual]">
              <xsl:element name="{@Qual}">
                  <xsl:attribute name="value">
                      <xsl:value-of select="source/input/@value" />
                  </xsl:attribute>
              </xsl:element>
          </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 2017-09-10
        • 2022-01-16
        • 1970-01-01
        • 2011-09-18
        • 1970-01-01
        • 2019-08-17
        • 2020-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多