XML-XSLT-保留输入XML文档的一部分

时间:2018-07-24 10:29:57

标签: xml xslt saxon xslt-3.0

我有这个输入的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文档?

谢谢!

Alexandre Jacinto

3 个答案:

答案 0 :(得分:1)

最简单的样式表可能是:

<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>

答案 1 :(得分:0)

您需要将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

答案 2 :(得分:0)

x <- c(1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9)
as.numeric(paste(x, collapse = ''))
# [1] 1.234568e+17
sum(x)
# 90
digitSumLarge(as.numeric(paste(x, collapse = '')))
# 85
digitsum(as.numeric(paste(x, collapse = '')))
# 81, with warning message about loss of accuracy