【问题标题】:Moving nodes down using XSLT使用 XSLT 下移节点
【发布时间】:2014-10-08 22:13:06
【问题描述】:

在这里提出我的问题后,我设法向上移动了一个节点 (Moving nodes up using XSLT)。在此之后,我认为我理解了它,所以我尝试相反的方式将节点向下移动。没用。这就是我所做的:

我的输入数据叫做 DEBTORS.xml:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 001" status="A" type="C">
  <Name>Name</Name>
  <Contacts>
   <Contact default="1" gender="M" status="A">
    <Note>Patient: 1</Note>
    <FirstName></FirstName>
    <Addresses>
     <Address type="D" desc="">
      <AddressLine1>Street</AddressLine1>
      <AddressLine2></AddressLine2>
      <AddressLine3></AddressLine3>
      <PostalCode>0000 AA</PostalCode>
      <City>&apos;City</City>
      <Country code="NL"/>
      <Phone></Phone>
      <Fax></Fax>
     </Address>
     </Addresses>
    <Language code="NL"/>
    <JobDescription>--</JobDescription>
    <Phone></Phone>
    <PhoneExt></PhoneExt>
    <Fax></Fax>
    <Mobile></Mobile>
    <Email></Email>
    <WebAccess>0</WebAccess>
     </Contact>
  </Contacts>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

我的 XSL 称为 Test2.xsl

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

<!-- Indentation in XSL -->
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

<!-- Removing blank lines in XSL -->
<xsl:strip-space  elements="*"/>

<!-- Identity rule -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

<!-- special rules ... -->
    <xsl:template match="Account">
        <xsl:copy>
          <!-- exclude Name -->
          <xsl:apply-templates select="@* | node()[not(self::Name)]"/>
        </xsl:copy>
</xsl:template>

<xsl:template match="Contacts">
    <xsl:copy>
        <!-- include Name -->
        <xsl:apply-templates select="@* | node() | Contact/Name"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

想要的输出:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 001" status="A" type="C">
  <Contacts>
   <Contact default="1" gender="M" status="A">
    <Name>Name</Name>
    <Note>Patient: 1</Note>
    <FirstName></FirstName>
    <Addresses>
     <Address type="D" desc="">
      <AddressLine1>Street</AddressLine1>
      <AddressLine2></AddressLine2>
      <AddressLine3></AddressLine3>
      <PostalCode>0000 AA</PostalCode>
      <City>&apos;City</City>
      <Country code="NL"/>
      <Phone></Phone>
      <Fax></Fax>
     </Address>
     </Addresses>
    <Language code="NL"/>
    <JobDescription>--</JobDescription>
    <Phone></Phone>
    <PhoneExt></PhoneExt>
    <Fax></Fax>
    <Mobile></Mobile>
    <Email></Email>
    <WebAccess>0</WebAccess>
     </Contact>
  </Contacts>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

我的问题是,在我的 XSL 中,节点“名称”被删除,但不会作为联系人的子节点返回。希望有人能帮助我吗?

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    我建议进行一些更改:

    1. 要禁止Name,添加一个与其匹配的模板规则,但不会 什么都没有。
    2. 要将Name 添加到Contact,请添加与Contact 匹配的模板规则和 照常复制,但也插入Name
    3. 消除当前匹配Contacts(复数)的模板;这 通用身份规则可以解决这个问题。

    这是您更新后的完整样式表:

    <xsl:stylesheet version="1.0" 
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <!-- Indentation in XSL -->
      <xsl:output method="xml" version="1.0" omit-xml-declaration="yes"
                  encoding="UTF-8" indent="yes"/>
    
      <!-- Removing blank lines in XSL -->
      <xsl:strip-space  elements="*"/>
    
      <!-- Identity rule -->
      <xsl:template match="@*|node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
      </xsl:template>
    
      <!-- special rules ... -->
      <xsl:template match="Name"/>
    
      <xsl:template match="Contact">
        <xsl:copy>
          <!-- include Name -->
          <xsl:apply-templates select="@*"/>
          <Name><xsl:value-of select="../../Name"/></Name>
          <xsl:apply-templates select="node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    根据您的示例输入 XML,上述 XSLT 会生成请求的输出 XML:

    <eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
       <Accounts>
          <Account code="                 001" status="A" type="C">
             <Contacts>
                <Contact default="1" gender="M" status="A">
                   <Name>Name</Name>
                   <Note>Patient: 1</Note>
                   <FirstName/>
                   <Addresses>
                      <Address type="D" desc="">
                         <AddressLine1>Street</AddressLine1>
                         <AddressLine2/>
                         <AddressLine3/>
                         <PostalCode>0000 AA</PostalCode>
                         <City>'City</City>
                         <Country code="NL"/>
                         <Phone/>
                         <Fax/>
                      </Address>
                   </Addresses>
                   <Language code="NL"/>
                   <JobDescription>--</JobDescription>
                   <Phone/>
                   <PhoneExt/>
                   <Fax/>
                   <Mobile/>
                   <Email/>
                   <WebAccess>0</WebAccess>
                </Contact>
             </Contacts>
             <Debtor number="   1" code="                 1">
                <Currency code="EUR"/>
             </Debtor>
          </Account>
       </Accounts>
    </eExact>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2010-10-26
      • 2021-09-05
      相关资源
      最近更新 更多