用于匹配特定父Complextype中的特定元素实例的模式的XSLT脚本

时间:2017-08-18 17:55:43

标签: xslt

我有一个类似这样的架构:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.28.0">  
  <xsd:complexType name="AccountsReceivableInfo_Type">
    <xsd:sequence>
      <xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>  
      <xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="ID"/>
    <xsd:attribute name="Locationref" type="IDref"/>
  </xsd:complexType>
 <xsd:complexType name="AccountsDeleteInfo_Type">
    <xsd:sequence>
      <xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>  
      <xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="ID"/>
    <xsd:attribute name="Locationref" type="IDref"/>
  </xsd:complexType>

我正在尝试修改它并编写一个XSLT脚本:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.28.0">  
  <xsd:complexType name="AccountsReceivableInfo_Type">
    <xsd:sequence>
    <!--<xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>-->  
      <xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="ID"/>
    <xsd:attribute name="Locationref" type="IDref"/>
  </xsd:complexType>
<xsd:complexType name="AccountsDeleteInfo_Type">
    <xsd:sequence>
      <xsd:element ref="SourceIncomePct" minOccurs="0" maxOccurs="1"/>  
      <xsd:element ref="DuplicateRecordsPct" minOccurs="0" maxOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="ID"/>
    <xsd:attribute name="Locationref" type="IDref"/>
  </xsd:complexType>

XSLT script which i wrote so far is
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/> 
<xsl:template match="xsd:complexType[@name='AccountsReceivableInfo_Type']
/xsd:element[@ref ='SourceIncomePct']">
     <xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>
     <xsl:sequence select="."/>
     <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
</xsl:template>
<xsl:template match="@*|node()">
     <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
</xsl:transform>

我要做的是修改上面的大模式并自动化它,并且需要读取特定复杂类型父级中的特定元素,并仅修改该实例,如注释掉它。

1 个答案:

答案 0 :(得分:0)

如果要将元素放入注释,请考虑转移到XSLT 3.0并使用

<xsl:template match="xsd:complexType[@name='AccountsReceivableInfo_Type']/xsd:sequence/xsd:element[@ref ='SourceIncomePct']">
  <xsl:comment select="serialize(.)"/>
</xsl:template>