用于转换xml的XSLT

时间:2016-07-16 06:44:31

标签: xml templates xslt xpath xml-parsing

我有一个XSLT文件,其中我想在该XSLT中进行更改,以便XML文件应该为教师转换主角色= yes,即使在主角色值= no的情况下传递XML。我可以通过通过将主角色设置为yes来编辑XML文件,但我想通过XSLT执行此操作。任何建议都会有所帮助

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE enterprise SYSTEM "ldisp-2.0.dtd">
<enterprise>
  <properties>
    <datasource>abcd  SCT Banner</datasource>
    <datetime>2016-07-08T10:45:23</datetime>
  </properties>
  <person>
    <sourcedid>
      <source>abcd - EAGLE SCT Banner</source>
      <id>884701</id>
    </sourcedid>
    <userid useridtype="Logon ID" pwencryptiontype="SSHA" password="*">bane</userid>
    <userid useridtype="SCTID" pwencryptiontype="SSHA" password="*">abdc345</userid>
    <userid useridtype="UDCIdentifier">154F041B95BB3EC9E0531600910A82B0</userid>
    <userid useridtype="Email ID">bane</userid>
    <name>
      <fn>Mr. bane poison</fn>
      <n>
        <family>bane</family>
        <given>Poison</given>
        <prefix>Mr.</prefix>
        <partname partnametype="MiddleName">T</partname>
      </n>
    </name>
    <demographics>
      <gender>2</gender>
    </demographics>
    <email>Stephen.@genome.eu</email>
    <***institutionrole primaryrole="No"*** institutionroletype="Faculty"/>
    <institutionrole primaryrole="No" institutionroletype="ProspectiveStudent"/>
    <institutionrole primaryrole="No" institutionroletype="Staff"/>
    <institutionrole primaryrole="No" institutionroletype="Student"/>
    <extension>
      <luminisperson>
<academicmajor>Cont Ed - Undeclared</academicmajor>
        <academicdegree>Master's Degree</academicdegree>
        <customrole>ApplicantAccept</customrole>
        <customrole>Empl</customrole>
        <customrole>Noncredit</customrole>
      </luminisperson>
    </extension>
  </person>
</enterprise>

XSLT文件

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes" version="1.0" encoding="UTF-8"/>

  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="datasource">
    <xsl:element name="{name()}">
      <xsl:text>BANNER</xsl:text>
    </xsl:element>
  </xsl:template>


  <xsl:template match="description/long">
    <xsl:element name="{name()}">
      <xsl:value-of select="../../sourcedid/id"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="description/full">
    <xsl:variable name="yearterm" select="substring-after(../../sourcedid/id,'.')" />
    <xsl:element name="{name()}">
      <xsl:value-of select="$yearterm"/><xsl:text> - </xsl:text>
      <xsl:value-of select="../long"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="timeframe"/>

<xsl:variable name="del" select="'IGNORE'" />
<xsl:variable name="deliv" select="enterprise/group/extension/luminisgroup/deliverysystem" />

  <xsl:template match="grouptype/typevalue">
    <xsl:if test ="contains($deliv, 'WEBCT')">
     <xsl:copy-of select="."/>
    </xsl:if>
    <xsl:if test ="not(contains($deliv, 'WEBCT'))">
     <xsl:element name="typevalue">
      <xsl:attribute name="level">
       <xsl:value-of select="1" />
      </xsl:attribute>
    <xsl:value-of select="$del"/>
     </xsl:element>
    </xsl:if>
  </xsl:template>

  <xsl:template match="person/name">
    <xsl:element name="{name()}">
      <xsl:apply-templates/>
    </xsl:element>

    <xsl:variable name="demographics" select="../demographics"/>
    <xsl:variable name="email"        select="../email"       />

    <xsl:if test="$demographics">
      <xsl:copy-of select="../demographics" />
    </xsl:if>

    <xsl:if test="not($email)">
      <email><xsl:text>noemail@genome.edu</xsl:text></email>
      <url><xsl:value-of select="../sourcedid/id"/></url>
    </xsl:if>
  </xsl:template>

  <xsl:template match="person/demographics"/>

  <xsl:template match="person/email">
    <xsl:element name="{name()}">
      <xsl:apply-templates/>
    </xsl:element>
    <url><xsl:value-of select="../sourcedid/id"/></url>
  </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

添加模板

<xsl:template match="institutionrole[@institutionroletype = 'Faculty']/@primaryrole">
  <xsl:attribute name="{name()}">Yes</xsl:attribute>
</xsl:template>