Xslt 在coldfusion中解析XML

Xslt 在coldfusion中解析XML,xslt,coldfusion,xml-parsing,Xslt,Coldfusion,Xml Parsing,我收到一个来自第三方的XML包- 他们提供了一个XSL来转换包的“部分” 我需要使用CF(10)从包中“提取”节点(带子节点) 包看起来是这样的(减小了大小-btu可以在主数据节点内包含多个“数据”节点…幸运的是,主数据节点有一个已知字符串的ID(“本例中的X_STUFF”)-因此我特别想要捕获带有子节点的节点 <trip> <eventTS>2012-09-19T14:54:42.0Z</eventTS> <eventX>0</eve

我收到一个来自第三方的XML包- 他们提供了一个XSL来转换包的“部分”

我需要使用CF(10)从包中“提取”节点(带子节点)

包看起来是这样的(减小了大小-btu可以在主数据节点内包含多个“数据”节点…幸运的是,主数据节点有一个已知字符串的ID(“本例中的X_STUFF”)-因此我特别想要捕获带有子节点的节点

<trip>
<eventTS>2012-09-19T14:54:42.0Z</eventTS>   
<eventX>0</eventX>
<eventY>10</eventY>
<eventType>driverRouteRequest</eventType>
<data id="X_STUFF">
    <datum name="A" value="..."/>
    <datum name="B" value="..."/>
    <data id="...">
        <datum name="a" value="0"/>
        <datum name="b" value="0"/>
        <datum name="c" value=""/>
    </data>
</data>
</trip>

2012-09-19T14:54:42.0Z
0
10
驱动程序请求
如果我尝试转换整个包,我会从第一个节点“trip”节点或任何没有属性的节点得到一个“零长度”字符串错误-我对XSL不太了解-但对我来说似乎是这样-我将在这里包含XSL只是为了提供信息-显然,是“问题”可以通过重新编写XSL来容纳不是“数据”节点的节点来解决(我没有将它们命名为data/datum,如果这混淆了事情的话,很抱歉)…或者我可以只拉“主数据”节点w/children…正如我上面建议的那样(从我的角度来看,这将更简单,更“可维护”——至少在我能够了解更多XSL之前)



将基准转换为元素v1.0
A.
A.
真的
假的

myxmldoc=XmlParse(“C:/ColdFusion10/cfusion/wwwroot/test.xml”);
myContent=XmlSearch(myxmldoc,“/*[@id='X_STUFF']”);
书面材料(myContent);

xPath字符串也是@Miguel-F的功劳

你说的“提取”是什么意思?你需要对这些数据做什么?你可以将这个文档读入内存,然后从那里解析它,如果你只是想得到结构或文本内容。James-我需要@jpmyob的文档数据-试试这个
xmlSearch(myXML,“//*[@id='X_STUFF']”
<?xml version="1.0" encoding="UTF-8"?>
<xsl:template match="//*">
    <xsl:comment>Translated with datum-to-elements v1.0</xsl:comment>
    <xsl:element name="returnMessage">
        <xsl:call-template name="processChildren">
        </xsl:call-template>
    </xsl:element>
</xsl:template>

<xsl:template name="processChildren">
    <xsl:param name="branch"/>
    <xsl:variable name="AValue" select="./datum[@name='A']/@value" />
    <xsl:variable name="aValue" select="./datum[@name='a']/@value" />

    <xsl:variable name="newElementName">
        <xsl:choose>
            <xsl:when test="string-length($AValue) > 0">
                <xsl:value-of select="$AValue"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$aValue"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="skipAttributeName">
        <xsl:choose>
            <xsl:when test="string-length($AValue) > 0">
                <xsl:text>A</xsl:text>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>a</xsl:text>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:element name="{$newElementName}">
        <xsl:for-each select="child::*">
            <xsl:choose>
                <xsl:when test="count(child::*) > 0 ">
                    <xsl:call-template name="processChildren">
                        <xsl:with-param name="branch" select="child::*"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:if test="@name != $skipAttributeName">
                        <xsl:element name="{@name}">
                            <xsl:choose>
                                <xsl:when test="@value = 'y' or @value = 'Y'">true</xsl:when>
                                <xsl:when test="@value = 'n' or @value = 'N'">false</xsl:when>
                                <xsl:otherwise>
                                    <xsl:value-of select="@value"/>
                                </xsl:otherwise>
                            </xsl:choose>
                        </xsl:element>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>

        </xsl:for-each>
    </xsl:element>

</xsl:template>
</xsl:stylesheet>
<cfscript> 

myxmldoc = XmlParse("C:/ColdFusion10/cfusion/wwwroot/test.xml"); 

myContent = XmlSearch(myxmldoc, "//*[@id='X_STUFF']"); 

writeDump(myContent);

</cfscript>