我正在尝试将XML文件转换为数组数组。 数组的第二个元素将包含来自root的节点路径,而rest元素包含相应的数据。
XSL:
<cfsavecontent variable="local.xsl">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<!--- Get all leaf nodes XPath: *[not(*)] --->
<xsl:key name="kNodeByPath" match="*[not(*)]"
use="concat(name(), '/', name(..), '/', name(../..), '/', name(../../..),
'/', name(../../../..), '/', name(../../../../..))"/>
<xsl:template match="*[not(*)][generate-id() = generate-id(key('kNodeByPath',
concat(name(), '/', name(..), '/', name(../..),
'/', name(../../..), '/', name(../../../..),
'/', name(../../../../..)))[1])]">
<xsl:apply-templates select="ancestor::*[parent::*]" mode="path"/>
<xsl:value-of select="name()"/>
<!--- Separate each path with comma --->
<xsl:text>,</xsl:text>
</xsl:template>
<xsl:template match="*" mode="path">
<xsl:value-of select="concat(name(), '.')"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
</cfsavecontent>
XML:
<?xml version="1.0" encoding="utf-8" ?>
<table>
<class>
<id>Test Data</id>
<title>Testing</title>
<description_url>Test Data</description_url>
<duration>2</duration>
<price>Test</price>
<instruction_language>Test Data</instruction_language>
<city>Online</city>
</class>
<class>
<id>Test Data</id>
<title>Testing</title>
<description_url>Test Data</description_url>
<duration>2</duration>
<price>Test</price>
<instruction_language>Test Data</instruction_language>
<city>Online</city>
</class>
<class>
<id>Test Data</id>
<title>Testing</title>
<description_url>Test Data</description_url>
<duration>2</duration>
<price>Test</price>
<instruction_language>Test Data</instruction_language>
<city>Online</city>
</class>
</table>
我的代码:
<!--- Get all distinct leaf node paths --->
<cfset local.xPath = xmlTransform(local.xml,local.xsl)>
<cfif structKeyExists(local,"xPath") AND len(local.xPath) GT 0>
<cfset local.xPath = listToArray(local.xPath,",")>
<cfset local.arrayData[2] = local.xPath>
<cfloop from="1" to="#arrayLen(local.xPath)#" index="local.currentPath" step="1">
<cfset local.nodePath = replaceNoCase(local.xPath[local.currentPath], ".", "/", "ALL")>
<!--- Search node in XML --->
<cfset local.xmlData[local.currentPath] = xmlSearch(local.xml, "//#local.nodePath#")>
</cfloop>
<!--- Get class node --->
<cfset local.class = xmlSearch(local.xml, "//class")>
<cfloop from="1" to="#arrayLen(local.class)#" index="local.row" step="1">
<cfloop from="1" to="#arrayLen(local.arrayData[2])#" index="local.column" step="1">
<cfset local.arrayData[local.row + 2][local.column] = local.xmlData[local.column][local.row].xmlText>
</cfloop>
</cfloop>
</cfif>
<cfdump var="#local.arrayData#">
实际和预期的产出:
请帮忙。提前谢谢。
答案 0 :(得分:-1)
<cfsavecontent variable="local.xml">
<?xml version="1.0" encoding="UTF-8"?>
<table>
<class>
<id>Test Data</id>
<title>Testing</title>
<description_url>Test Data</description_url>
<duration>2</duration>
<price>Test</price>
<instruction_language>Test Data</instruction_language>
<city>Online</city>
</class>
<class>
<id>Test Data</id>
<title>Testing</title>
<description_url>Test Data</description_url>
<duration>2</duration>
<price>Test</price>
<instruction_language>Test Data</instruction_language>
<city>Online</city>
</class>
<class>
<id>Test Data</id>
<title>Testing</title>
<description_url>Test Data</description_url>
<duration>2</duration>
<price>Test</price>
<instruction_language>Test Data</instruction_language>
<city>Online</city>
</class>
</table>
</cfsavecontent>
<cfset local.XmlParseData = XmlParse(trim(local.xml))>
<cfdump var="#local.XmlParseData#"><cfabort>
&#13;