首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT中XML到XML的转换

XSLT中XML到XML的转换
EN

Stack Overflow用户
提问于 2020-03-31 16:37:56
回答 1查看 51关注 0票数 0

我有一个xml文档,我试图通过分组和计数将其转换为不同的xml文档。我曾经尝试过文本,因为我在过去使用过它,一切正常工作,但我试图转换为xml,没有看到预期的结果。

错误:名称空间前缀'xd‘尚未声明

xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xd:StudentData xmlns:xd="urn:com.student.report/student_test">
   <xd:StudentRecord>
      <xd:StudentRequisition xd:Descriptor="1858 Economics"/>
      <xd:StudentStatus xd:Descriptor="Pass"/>
      <xd:StudentApplication xd:Descriptor="1858 Economics"/>
      <xd:StudentID>S-1</xd:StudentID>
      <xd:Gender xd:Descriptor="Male"/>
      <xd:StudentDate>2020-01-01-12:00</xd:StudentDate>
      <xd:Total_Score>75</xd:Total_Score>
      <xd:ProfileStudentTestGroup>
         <xd:StudentTest xd:Descriptor="Student Evaluation"/>
         <xd:StudentTest_Status xd:Descriptor="Pass"/>
         <xd:StudentTest_Date>2020-01-31-12:00</xd:StudentTest_Date>
         <xd:StudentTest_Result_Score>75</xd:StudentTest_Result_Score>
      </xd:ProfileStudentTestGroup>
   </xd:StudentRecord>
   <xd:StudentRecord>
      <xd:StudentRequisition xd:Descriptor="1500 Social Service"/>
      <xd:StudentStatus xd:Descriptor="Fail"/>
      <xd:StudentApplication xd:Descriptor="1500 Social Service"/>
      <xd:StudentID>S-3</xd:StudentID>
      <xd:Gender xd:Descriptor="Female"/>
      <xd:StudentDate>2020-01-01-12:00</xd:StudentDate>
      <xd:Total_Score>65</xd:Total_Score>
      <xd:ProfileStudentTestGroup>
         <xd:StudentTest xd:Descriptor="Student Evaluation"/>
         <xd:StudentTest_Status xd:Descriptor="Pass"/>
         <xd:StudentTest_Date>2020-01-31-12:00</xd:StudentTest_Date>
         <xd:StudentTest_Result_Score>65</xd:StudentTest_Result_Score>
      </xd:ProfileStudentTestGroup>
   </xd:StudentRecord>
   <xd:StudentRecord>
      <xd:StudentRequisition xd:Descriptor="1500 Social Service"/>
      <xd:StudentStatus xd:Descriptor="Fail"/>
      <xd:StudentApplication xd:Descriptor="1500 Social Service"/>
      <xd:StudentID>S-4</xd:StudentID>
      <xd:Gender xd:Descriptor="Female"/>
      <xd:StudentDate>2020-01-01-12:00</xd:StudentDate>
      <xd:Total_Score>67</xd:Total_Score>
      <xd:ProfileStudentTestGroup>
         <xd:StudentTest xd:Descriptor="Student Evaluation"/>
         <xd:StudentTest_Status xd:Descriptor="Pass"/>
         <xd:StudentTest_Date>2020-01-31-12:00</xd:StudentTest_Date>
         <xd:StudentTest_Result_Score>67</xd:StudentTest_Result_Score>
      </xd:ProfileStudentTestGroup>
   </xd:StudentRecord>
   <xd:StudentRecord>
      <xd:StudentRequisition xd:Descriptor="1858 Economics"/>
      <xd:StudentStatus xd:Descriptor="Pass"/>
      <xd:StudentApplication xd:Descriptor="1858 Economics"/>
      <xd:StudentID>S-7</xd:StudentID>
      <xd:Gender xd:Descriptor="Male"/>
      <xd:StudentDate>2020-01-01-12:00</xd:StudentDate>
      <xd:Total_Score>85</xd:Total_Score>
      <xd:ProfileStudentTestGroup>
         <xd:StudentTest xd:Descriptor="Student Evaluation"/>
         <xd:StudentTest_Status xd:Descriptor="Pass"/>
         <xd:StudentTest_Date>2020-01-31-12:00</xd:StudentTest_Date>
         <xd:StudentTest_Result_Score>85</xd:StudentTest_Result_Score>
      </xd:ProfileStudentTestGroup>
   </xd:StudentRecord>
</xd:StudentData>

xslt:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    xmlns:xd="urn:com.student.report/student_test"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/*">
<root>
  <xsl:for-each-group select="StudentRecord" composite="yes" group-by="StudentRequisition/@xd:Descriptor, ProfileStudentTestGroup/StudentTest/@xd:Descriptor">
  <StudentRequisition>
      <xsl:text>{current-grouping-key()[1]}</xsl:text>
      </StudentRequisition>
  <StudentTest>
      <xsl:text>{current-grouping-key()[2]}</xsl:text>
      </StudentTest>  
  <MPass>
      <xsl:text>{count(current-group()[Gender/@xd:Descriptor = 'Male'][ProfileStudentTestGroup/StudentTest_Status[@xd:Descriptor = 'Pass']])}</xsl:text>
      </MPass>
  </xsl:for-each-group>
</root>
  </xsl:template>
</xsl:stylesheet>

输出:

代码语言:javascript
复制
<root>
<StudentRequisition>1858 Economics</StudentRequisition>
<StudentTest>Student Evaluation</StudentTest>
<MPass>1</MPass>
</root>
EN

回答 1

Stack Overflow用户

发布于 2020-03-31 16:43:33

在XSLT中,您将名称空间前缀声明为wd,可能是一个错误:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    xmlns:wd="urn:com.student.report/student_test"
          ^
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60955509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档