-1

使用下面的 xml,您能否帮助我完成以下操作所需的 xsl 转换代码:

当前的 XML:

<ROOTNODE>
  <SUBNODE1>
<DETAILS>
  <SOMETHING>Here</SOMETHING>
  <UNIMPORTANT1>Thing</UNIMPORTANT1>
</DETAILS>
<SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
<ALSOUNIMPORTANT>Very</ALSOUNIMPORTANT>
  </SUBNODE1>
</ROOTNODE>

输出 XML:

<DETAILS>
  <SOMETHING>Here</SOMETHING>
</DETAILS>
<SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
4

2 回答 2

1

这种转变

<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="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*|/*/*"><xsl:apply-templates/></xsl:template>
 <xsl:template match="*[contains(name(), 'UNIMPORTANT')]"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<ROOTNODE>
    <SUBNODE1>
        <DETAILS>
            <SOMETHING>Here</SOMETHING>
            <UNIMPORTANT1>Thing</UNIMPORTANT1>
        </DETAILS>
        <SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
        <ALSOUNIMPORTANT>Very</ALSOUNIMPORTANT>
    </SUBNODE1>
</ROOTNODE>

产生想要的正确结果

<DETAILS>
   <SOMETHING>Here</SOMETHING>
</DETAILS>
<SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
于 2012-10-18T23:07:35.397 回答
0

你可以试试:

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

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

    <xsl:template match="/">
        <newroot>
            <DETAILS>
                <xsl:copy-of select="//DETAILS/SOMETHING"/>
            </DETAILS>
            <xsl:copy-of select="//SOMEWHATIMPORTANT"/>
        </newroot>
    </xsl:template>

</xsl:stylesheet>

更新

我使用以下 ANT 项目来运行这个名为transform.xml的样式表

.
├── build.xml
├── data.xml
└── transform.xsl

运行该项目会产生以下输出:

$ ant && cat build/data.xml
Buildfile: /home/mark/tmp/build.xml

transform:
     [xslt] Processing /home/mark/tmp/data.xml to /home/mark/tmp/build/data.xml
     [xslt] Loading stylesheet /home/mark/tmp/transform.xsl

BUILD SUCCESSFUL
Total time: 0 seconds
<?xml version="1.0" encoding="UTF-8"?>
<newroot>
<DETAILS>
<SOMETHING>Here</SOMETHING>
</DETAILS>
<SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
</newroot>

构建.xml

<project name="xslt-demo" default="transform">

    <target name="transform">
        <xslt style="transform.xsl" in="data.xml" out="build/data.xml"/>
    </target>

    <target name="clean">
        <delete dir="build"/>
    </target>

</project>

数据.xml

<ROOTNODE>
  <SUBNODE1>
<DETAILS>
  <SOMETHING>Here</SOMETHING>
  <UNIMPORTANT1>Thing</UNIMPORTANT1>
</DETAILS>
<SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
<ALSOUNIMPORTANT>Very</ALSOUNIMPORTANT>
  </SUBNODE1>
</ROOTNODE>
于 2012-10-18T22:42:16.773 回答