Xslt 通过对字段进行分组来转换XML
我对XSLT的知识有限,但总是渴望学习。我目前正在开发一个需要转换XML输入的模板。我一直在尝试对InvoiceNum字段进行分组,但没有任何结果。 我收到一个错误:Envision.Utilities.xsltenine-Object引用未设置为对象的实例 以下是供参考的输入XML:Xslt 通过对字段进行分组来转换XML,xslt,Xslt,我对XSLT的知识有限,但总是渴望学习。我目前正在开发一个需要转换XML输入的模板。我一直在尝试对InvoiceNum字段进行分组,但没有任何结果。 我收到一个错误:Envision.Utilities.xsltenine-Object引用未设置为对象的实例 以下是供参考的输入XML: <?xml version='1.0' ?> <Request> <Information> <ImageID>987456321</Imag
<?xml version='1.0' ?>
<Request>
<Information>
<ImageID>987456321</ImageID>
<Contract>123456789</Contract>
<Lastname>MICKEYMOUSE</Lastname>
</Information>
<Document>
<InvoiceNum>123456823</InvoiceNum>
<Reference>AD20985224</Reference>
<InvoiceNum>100000123</InvoiceNum>
<Reference>AS20101387</Reference>
<InvoiceNum>858511825</InvoiceNum>
<Reference>GF96844</Reference>
<InvoiceNum>885154145</InvoiceNum>
<Reference>FGFD2018</Reference>
<InvoiceNum>25241111</InvoiceNum>
<Reference>SD88888</Reference>
<InvoiceNum>8571414</InvoiceNum>
<Reference>DF864841254</Reference>
</Document>
</Request>
以下是我的XSLT格式供参考:
我错过了什么?有没有更好的方法来格式化我目前在下面使用的XSLT模板?非常感谢您的帮助
<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:param name="cols" select="3" />
<xsl:template match="Request">
<table border="1">
<xsl:apply-templates select="InvoiceNum[position() mod $cols = 1]"/>
</table>
</xsl:template>
<xsl:template match="Document">
<xsl:variable name="group" select=". | following-sibling::InvoiceNum
[position() < $cols]" />
<xsl:for-each select="*">
<xsl:variable name="i" select="position()" />
<Invoice>
<InvoiceNumber>
<xsl:value-of select="InvoiceNum()"/>
</InvoiceNumber>
<xsl:for-each select="$group">
<xsl:value-of select="*[$i]"/>
</xsl:for-each>
</Invoice>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
下面是我想要的XML输出:
<InvCase>
<Invoices>
<InvoicemNumber>InvoiceNum1</InvoicemNumber>
<InvoicemNumber>InvoiceNum2</InvoicemNumber>
<InvoicemNumber>InvoiceNum3</InvoicemNumber>
</Invoices>
</InvCase>
看起来您正试图在InvoiceNum中将其分组为3人一组。您遇到的第一个问题是,在模板匹配请求中,您执行以下操作
<xsl:apply-templates select="InvoiceNum[position() mod $cols = 1]"/>
谢谢蒂姆一个接一个的解释。那有帮助!我试图执行Xslt,但得到的错误对象引用未设置为对象的实例。。。。尝试搜索需要删除或添加的内容。我怀疑错误不是由于XSLT本身,而是由于您如何将XSLT应用于XML或如何尝试处理输出。有关XSLT本身的一个示例,请参见。我正在尝试转换输入XML以将所有XML分组到标记下。假设XML输入总共有6到10个字段。最后的XML输出应该包含所有6个或10个标记,每个字段的值都不同。这是我XSLT模板的其余部分,我被困在如何填充和分组并在多个下填充上。这是我XSLT模板的其余部分,我被困在如何填充和分组所有并在多个下填充上标签
<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:param name="cols" select="3" />
<xsl:template match="Request">
<InvCases>
<xsl:apply-templates select="Document/InvoiceNum[position() mod $cols = 1]"/>
</InvCases>
</xsl:template>
<xsl:template match="InvoiceNum">
<xsl:variable name="group" select=". | following-sibling::InvoiceNum[position() < $cols]" />
<Invoice>
<xsl:for-each select="$group">
<InvoiceNumber>
<xsl:value-of select="."/>
</InvoiceNumber>
</xsl:for-each>
</Invoice>
</xsl:template>
</xsl:stylesheet>