为什么我的XSL在这个XML中找不到要处理的东西?我试图将cognos xml报告输出转换为另一种形式(是的,我知道cognos生成html,但我还需要其他东西)。它只生成“Caption:”一词并停止。
这是我的XSL:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<xsl:template match="/">
<xsl:template match="rows">
<xsl:apply-templates select="rowEdge"/>
</xsl:template>
<xsl:template match="rowEdge">
<b>Caption:</b> <xsl:value-of select="caption"/><br/>
<xsl:if test="@id">
<b>id:</b> <xsl:value-of select="@id"/><br/>
</xsl:if>
<xsl:if test="rowEdge">
<xsl:apply-templates select="rowEdge"/>
</xsl:if>
</xsl:template>
<html>
<head><title>%s</title></head>
<body>
<xsl:apply-templates select="dataset/crosstab/rows"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这是XML(我必须删除一些,但应该提出一个想法):
<?xml version="1.0" encoding="utf-8" ?>
<dataset xmlns="http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
<crosstab>
<values>
<value row="R1" col="C1" xs:nil="true" />
<value row="R1" col="C2" xs:nil="true" />
<value row="R1" col="C3" xs:nil="true" />
<value row="R1" col="C4" xs:nil="true" />
<value row="R1" col="C5" xs:nil="true" />
<value row="R1" col="C6" xs:nil="true" />
<value row="R1" col="C7" xs:nil="true" />
<value row="R1" col="C8" xs:nil="true" />
<value row="R1" col="C9" xs:nil="true" />
<value row="R2" col="C1">0</value>
<value row="R2" col="C2">12.61728395</value>
<value row="R2" col="C3">1320.40677966</value>
<value row="R2" col="C4">7</value>
<value row="R2" col="C5">1.90318499</value>
<value row="R2" col="C6">108.66456135</value>
<value row="R2" col="C7">776.61407946</value>
<value row="R2" col="C8">-0.86007907</value>
<value row="R2" col="C9">46.53571429</value>
</values>
<corner>
<caption>Time Period: DCG Incurred Paid Year</caption>
</corner>
<columns>
<colEdge>
<caption>Jan 02 to Dec 02 {All Data} CRxIp</caption>
<colEdge>
<caption>2002</caption>
<colEdge id="C1">
<caption>Member Age Avg</caption>
</colEdge>
<colEdge id="C2">
<caption>Days Supply Per Script Rx</caption>
</colEdge>
<colEdge id="C3">
<caption>Net Pay Per Pat Med</caption>
</colEdge>
<colEdge id="C4">
<caption>Days LOS Admit Acute</caption>
</colEdge>
<colEdge id="C5">
<caption>Days LOS Admit Acute</caption>
</colEdge>
<colEdge id="C6">
<caption>Allow Amt PMPM Med and Rx {Cmpl}</caption>
</colEdge>
<colEdge id="C7">
<caption>Allow Amt PMPM Med and Rx {Cmpl}</caption>
</colEdge>
<colEdge id="C8">
<caption>% Diff Allow Amt PMPM Med and Rx {Cmpl}</caption>
</colEdge>
<colEdge id="C9">
<caption>Relative Risk Score Prosp Explan NonRescaled</caption>
</colEdge>
</colEdge>
</colEdge>
</columns>
<rows>
<rowEdge>
<caption>Subsets</caption>
<rowEdge>
<caption>Plan Type Medstat</caption>
<rowEdge id="R1">
<caption>Cohort Medstat</caption>
</rowEdge>
</rowEdge>
</rowEdge>
<rowEdge>
<caption>Chronic Episodes</caption>
<rowEdge>
<caption>HMO (Managed Care)</caption>
<rowEdge id="R2">
<caption>Females, Ages < 1</caption>
</rowEdge>
</rowEdge>
</rowEdge>
</rows>
</crosstab>
</dataset>
答案 0 :(得分:0)
在XSLT 1.0(您正在使用)中,XPath表达式中不考虑默认名称空间。
你需要做的是:
1)在XSLT转换中为实例文档的默认命名空间分配前缀,例如:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="cog:http://developer.cognos.com/schemas/xmldata/1/" xmlns:xs="http://www.w3.org/2001/XMLSchema-instance">
2)在XPath表达式中的每个元素名称之前(但不是在属性名称之前)使用此前缀:
<xsl:template match="cog:rowEdge">
<b>Caption:</b> <xsl:value-of select="cog:caption"/><br/>
<xsl:if test="@id">
<b>id:</b> <xsl:value-of select="@id"/><br/>
</xsl:if>
<xsl:if test="cog:rowEdge">
<xsl:apply-templates select="cog:rowEdge"/>
</xsl:if>
</xsl:template>
注意:在应用模板之前,您不需要测试是否存在cog:rowEdge
:如果其选择属性中没有任何节点,则apply-templates将不会执行任何操作;)...
样式表中还有其他一些错误:
xsl:stylesheet
元素中必须有一个版本属性。正确的样式表是:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cog="http://developer.cognos.com/schemas/xmldata/1/" version="1.0">
<xsl:template match="/">
<html>
<head>
<title>%s</title>
</head>
<body>
<xsl:apply-templates select="cog:dataset/cog:crosstab/cog:rows"/>
</body>
</html>
</xsl:template>
<xsl:template match="cog:rows">
<xsl:apply-templates select="cog:rowEdge"/>
</xsl:template>
<xsl:template match="cog:rowEdge">
<b>Caption:</b>
<xsl:value-of select="cog:caption"/>
<br/>
<xsl:if test="@id">
<b>id:</b>
<xsl:value-of select="@id"/>
<br/>
</xsl:if>
<xsl:apply-templates select="cog:rowEdge"/>
</xsl:template>
</xsl:stylesheet>