0

我在尝试处理具有递归标签的 XML 时遇到了两个问题(相同的标签在标签内重复)。

问题 #1 “标题:”部分输出所有标题标签的文本连接,就好像它是从 / 而不是调用它的路径搜索。

问题 #2 @id 测试什么也没找到,这可能是同一问题的症状。

这是 XSL:

<?xml version="1.0"?>
<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"> 
<xsl:template match="/">
<xsl:template match="cog:rows">
    <xsl:apply-templates select="cog:rowEdge"/>
</xsl:template>
<xsl:template match="cog:rowEdge">
    <p><b>Caption:</b> <xsl:value-of select="cog:caption"/></p>
    <xsl:if test="@id">
        <b>id:</b> <xsl:value-of select="@id"/><br/>
    </xsl:if>
    <xsl:if test="cog:rowEdge">
        <p>Looking at next rowEdge</p>
        <xsl:apply-templates select="cog:rowEdge"/>
    </xsl:if>
</xsl:template>
<html>
<head><title>%s</title></head>
<body>
    <xsl:apply-templates select="cog:dataset/cog:crosstab/cog: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 &lt; 1</caption>  
  </rowEdge> 
</rowEdge> 
 </rowEdge> 
 </rows> 
  </crosstab> 
  </dataset> 
4

1 回答 1

0

您的样式表无效,因为文档节点的模板包含所有其他模板。模板是 XSLT 中的顶级元素,不能嵌套。如果将它们分开,样式表应该按预期工作:

<xsl:stylesheet version="1.0" 
   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"> 

   <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/>
   </xsl:template>

   <xsl:template match="cog:rowEdge">
      <p><b>Caption:</b> <xsl:value-of select="cog:caption"/></p>
      <xsl:if test="@id">
         <b>id:</b> <xsl:value-of select="@id"/><br/>
      </xsl:if>
      <xsl:if test="cog:rowEdge">
         <p>Looking at next rowEdge</p>
         <xsl:apply-templates select="cog:rowEdge"/>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet> 
于 2012-05-22T18:57:59.053 回答