XML - XSLT - 保留输入 XML 文档的一部分
XML - XSLT - Keeping part of the input XML document
我有这个输入 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<jasperPrint xmlns="http://jasperreports.sourceforge.net/jasperreports/print"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd">
<page>
<firstElement>Some Data</firstElement>
<secondElement name="2ndElement">
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 1</secondChild>
</subElement>
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 2</secondChild>
</subElement>
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 3</secondChild>
</subElement>
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 4</secondChild>
</subElement>
</secondElement>
</page>
</jasperPrint>
我想使用 XSLT 对其进行转换,以便仅保留 <secondChild>
元素,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<secondChildList>
<secondChild>DATA I WANT TO KEEP 1</secondChild>
<secondChild>DATA I WANT TO KEEP 2</secondChild>
<secondChild>DATA I WANT TO KEEP 3</secondChild>
<secondChild>DATA I WANT TO KEEP 4</secondChild>
</secondChildList>
这是我正在尝试的 XSLT 代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://jasperreports.sourceforge.net/jasperreports/print"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="jasperPrint/page/secondElement">
<secondChildList>
<xsl:for-each select="subElement">
<secondChild>
<xsl:value-of select="secondChild"/>
</secondChild>
</xsl:for-each>
</secondChildList>
</xsl:template>
</xsl:stylesheet>
这只是输出输入 XML 文档中的文本,如下所示:
<?xml version="1.0" encoding="UTF-8"?>Some DataDATA I WANT TO KEEP 1DATA I WANT TO KEEP 2DATA I WANT TO KEEP 3DATA I WANT TO KEEP 4
如何使用 XSLT 实现我想要的输出 XML 文档?
谢谢!
亚历山大·哈辛托
您需要将 for-each
更改为
<xsl:for-each select="subElement/secondChild">
<secondChild>
<xsl:value-of select="."/>
</secondChild>
</xsl:for-each>
或更好地使用
<xsl:apply-templates select="subElement/secondChild">
而不是 for-each
。
实现此目的的最简单样式表可能是:
<secondChildList xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:copy-of select="//secondChild"/>
</secondChildList>
试一试。没有 xsl:stylesheet
包装器的简化样式表对于像这样的简单工作来说非常方便。但是如果你想添加像 xsl:output 这样的东西,那么你需要完整的语法:
<xsl:stylesheet xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<secondChildList>
<xsl:copy-of select="//secondChild"/>
</secondChildList>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://jasperreports.sourceforge.net/jasperreports/print"
exclude-result-prefixes="xs m"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="m:page">
<secondChildLis>
<xsl:for-each select="//m:secondChild">
<secondChild>
<xsl:apply-templates/>
</secondChild>
</xsl:for-each>
</secondChildLis>
</xsl:template>
</xsl:stylesheet>
You may also try it.
我有这个输入 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<jasperPrint xmlns="http://jasperreports.sourceforge.net/jasperreports/print"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd">
<page>
<firstElement>Some Data</firstElement>
<secondElement name="2ndElement">
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 1</secondChild>
</subElement>
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 2</secondChild>
</subElement>
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 3</secondChild>
</subElement>
<subElement id="1SE">
<firstChild name="1st"/>
<secondChild>DATA I WANT TO KEEP 4</secondChild>
</subElement>
</secondElement>
</page>
</jasperPrint>
我想使用 XSLT 对其进行转换,以便仅保留 <secondChild>
元素,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<secondChildList>
<secondChild>DATA I WANT TO KEEP 1</secondChild>
<secondChild>DATA I WANT TO KEEP 2</secondChild>
<secondChild>DATA I WANT TO KEEP 3</secondChild>
<secondChild>DATA I WANT TO KEEP 4</secondChild>
</secondChildList>
这是我正在尝试的 XSLT 代码:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://jasperreports.sourceforge.net/jasperreports/print"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/print http://jasperreports.sourceforge.net/xsd/jasperprint.xsd">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="jasperPrint/page/secondElement">
<secondChildList>
<xsl:for-each select="subElement">
<secondChild>
<xsl:value-of select="secondChild"/>
</secondChild>
</xsl:for-each>
</secondChildList>
</xsl:template>
</xsl:stylesheet>
这只是输出输入 XML 文档中的文本,如下所示:
<?xml version="1.0" encoding="UTF-8"?>Some DataDATA I WANT TO KEEP 1DATA I WANT TO KEEP 2DATA I WANT TO KEEP 3DATA I WANT TO KEEP 4
如何使用 XSLT 实现我想要的输出 XML 文档?
谢谢!
亚历山大·哈辛托
您需要将 for-each
更改为
<xsl:for-each select="subElement/secondChild">
<secondChild>
<xsl:value-of select="."/>
</secondChild>
</xsl:for-each>
或更好地使用
<xsl:apply-templates select="subElement/secondChild">
而不是 for-each
。
实现此目的的最简单样式表可能是:
<secondChildList xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:copy-of select="//secondChild"/>
</secondChildList>
试一试。没有 xsl:stylesheet
包装器的简化样式表对于像这样的简单工作来说非常方便。但是如果你想添加像 xsl:output 这样的东西,那么你需要完整的语法:
<xsl:stylesheet xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<secondChildList>
<xsl:copy-of select="//secondChild"/>
</secondChildList>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:m="http://jasperreports.sourceforge.net/jasperreports/print"
exclude-result-prefixes="xs m"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="m:page">
<secondChildLis>
<xsl:for-each select="//m:secondChild">
<secondChild>
<xsl:apply-templates/>
</secondChild>
</xsl:for-each>
</secondChildLis>
</xsl:template>
</xsl:stylesheet>
You may also try it.