使用下面的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>
答案 0 :(得分: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>
答案 1 :(得分: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>
<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>
<ROOTNODE>
<SUBNODE1>
<DETAILS>
<SOMETHING>Here</SOMETHING>
<UNIMPORTANT1>Thing</UNIMPORTANT1>
</DETAILS>
<SOMEWHATIMPORTANT>This</SOMEWHATIMPORTANT>
<ALSOUNIMPORTANT>Very</ALSOUNIMPORTANT>
</SUBNODE1>
</ROOTNODE>