每天早上将多个xml文件转储到一个文件夹中,每个文件包含一个记录。这些文件中的每一个都有近300个节点,但我只需发送大约20条信息。因此,出于显而易见的原因,我想仅提取所需数据而不是删除不需要的数据。我一直试图用xslt做到这一点,但不能完全正确。我已经尝试了很多不同的模板,我不会在这里发布它们。相反,我只是举一个源xml的例子以及我需要的输出xml。
Source.xml:
<?xml version="1.0"?>
<NewDataSet xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Report>
<Overview>
<Agency>Agengcy1</Agency>
<AgencyNumber>2346</AgencyNumber>
<ReportDate>2017-07-24</ReportDate>
</Overview>
<Summary>
<ReportNumber>17-092447</ReportNumber>
<Boxes>2</Boxes>
<Crates>1</Crates>
</Summary>
<Unit>
<Order>
<LastName>SMITH</LastName>
<FirstName>JOHN</FirstName>
<Address>123 MAIN</Address>
<Floor>2</Floor>
<State>IL</State>
<City>CHICAGO</City>
<Zip>60007</Zip>
</Order>
</Unit>
<Unit>
<Order>
<LastName>SMITH</LastName>
<FirstName>JANE</FirstName>
<Address>123 MAIN</Address>
<Floor>7</Floor>
<State>IL</State>
<City>CHICAGO</City>
<Zip>60007</Zip>
</Order>
</Unit>
</Report>
</NewDataSet>
的Output.xml:
<?xml version="1.0"?>
<Report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Agency>Agengcy1</Agency>
<ReportDate>2017-07-24</ReportDate>
<ReportNumber>17-092447</ReportNumber>
<Unit>
<LastName>SMITH</LastName>
<FirstName>JOHN</FirstName>
<Floor>2</Floor>
</Unit>
<Unit>
<LastName>SMITH</LastName>
<FirstName>JANE</FirstName>
<Floor>7</Floor>
</Unit>
</Report>
我的xslt应该如何获取output.xml,包括缩进?提前谢谢
修改 我尝试了以下但它在输出中留下了空格。此外,一旦我意识到我将不得不添加近300个xsl:template match =“”语句,我就停止了。 这样:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="AgencyNumber"/>
<xsl:template match="Boxes"/>
<xsl:template match="Crates"/>
<xsl:template match="Address"/>
<xsl:template match="State"/>
<xsl:template match="City"/>
<xsl:template match="Zip"/>
</xsl:stylesheet>
给我这个:
<Report>
<Overview>
<Agency>Agency1</Agency>
<ReportDate>2017-07-24</ReportDate>
</Overview>
<Summary>
<ReportNumber>17-092447</ReportNumber>
<Unit>
<Order>
<LastName>SMITH</LastName>
<FirstName>JOHN</FirstName>
<Floor>2</Floor>
</Order>
</Unit>
</Report>
第二次更新 我也用过这个:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/NewDataSet/Report">
<Report>
<Agcy><xsl:value-of select="Overview/Agency" /></Agcy>
<Date><xsl:value-of select="Overview/ReportDate" /></Date>
<RprtNbr><xsl:value-of select="Summary/ReportNumber" /></RprtNbr>
<Unit>
<Last><xsl:value-of select="Unit/Order/LastName" /></Last>
<First><xsl:value-of select="Unit/Order/FirstName" /></First>
<Floor><xsl:value-of select="Unit/Order/Floor" /></Floor>
</Unit>
</Report>
</xsl:template>
</xsl:stylesheet>
但它是这样的:
<?xml version="1.0" encoding="utf-8"?>
<Report><Agcy>Agengcy1</Agcy><Date>2017-07-24</Date><RprtNbr>17-092447</RprtNbr><Unit><Last>SMITH</Last><First>JOHN</First><Floor>2</Floor></Unit></Report>
答案 0 :(得分:0)
不要删除你不想要的部分,而是尝试只获得你想要的部分:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Report">
<xsl:copy>
<xsl:copy-of select="Overview/Agency | Overview/ReportDate | Summary/ReportNumber"/>
<xsl:for-each select="Unit">
<xsl:copy>
<xsl:copy-of select="Order/LastName | Order/FirstName | Order/Floor"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
或者,如果您愿意:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/NewDataSet">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Overview">
<xsl:apply-templates select="Agency | ReportDate"/>
</xsl:template>
<xsl:template match="Summary">
<xsl:apply-templates select="ReportNumber"/>
</xsl:template>
<xsl:template match="Order">
<xsl:apply-templates select="LastName | FirstName | Floor"/>
</xsl:template>
</xsl:stylesheet>