使用 XSLT 向上移动节点 [英] Moving nodes up using XSLT

查看:42
本文介绍了使用 XSLT 向上移动节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了很多搜索,但我无法弄清楚如何准确地使用模板.

我的输入数据称为 DEBTORS.xml:

<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd"><账户><账户代码=001"状态=A"类型=C"><姓名>姓名</姓名><联系人><联系默认=1"性别=M"状态=A"><注意>患者:1</注意><名字></名字><地址><地址类型="D" desc=""><AddressLine1>街道</AddressLine1><AddressLine2></AddressLine2><AddressLine3></AddressLine3><邮政编码>0000 AA</邮政编码><城市>&‘城市</城市><国家代码=NL"/><电话></电话><传真></传真></地址></地址><语言代码="NL"/><工作描述>--</工作描述><电话></电话><PhoneExt></PhoneExt><传真></传真><移动></移动><电子邮件></电子邮件><WebAccess>0</WebAccess></联系方式></联系人><债务人编号=1"代码=1"><货币代码="欧元"/></债务人></帐户></帐户></eExact>

我的 XSL 称为 Test.xsl

<!-- XSL 中的缩进 --><xsl:output omit-xml-declaration="yes" indent="yes"/><!-- 在 XSL 中删除空行 --><xsl:strip-space elements="*"/><!-- 身份规则--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><!-- 特殊规则...--><xsl:template match="联系人"><xsl:copy><!--应用当前节点的属性和所有的属性孩子们--><xsl:apply-templates select="@* | child::node()[not(self::Note)]"/></xsl:copy><xsl:apply-templates select="Note"/></xsl:模板></xsl:stylesheet>

想要的输出:

<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd"><账户><账户代码=868"状态=A"类型=C"><姓名>姓名</姓名><联系人><联系默认=1"性别=M"状态=A"><名字></名字><地址><地址类型="D" desc=""><AddressLine1>街道</AddressLine1><AddressLine2></AddressLine2><AddressLine3></AddressLine3><邮政编码>0000 AA</邮政编码><城市>&‘城市</城市><国家代码=NL"/><电话></电话><传真></传真></地址></地址><语言代码="NL"/><工作描述>--</工作描述><电话></电话><PhoneExt></PhoneExt><传真></传真><移动></移动><电子邮件></电子邮件><WebAccess>0</WebAccess></联系方式></联系人><注意>患者:1</注意><债务人编号=1"代码=1"><货币代码="欧元"/></债务人></帐户></帐户></eExact>

我的问题是,对于我的 XSL,节点注意"作为联系人的子节点出现,但我希望它作为帐户的子节点.希望有人能帮助我?

解决方案

我的问题是,在我的 XSL 中,节点Note"是联系方式,但我希望将其作为帐户的子项.

那么,您需要将其从 Contact排除,并将其包含Account 中:

XSLT 1.0

<xsl:strip-space elements="*"/><xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/><!-- 身份规则--><xsl:template match="@*|node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><!-- 特殊规则...--><xsl:template match="联系人"><xsl:copy><!-- 排除注意--><xsl:apply-templates select="@* | node()[not(self::Note)]"/></xsl:copy></xsl:模板><xsl:template match="帐户"><xsl:copy><!-- 包括注释--><xsl:apply-templates select="@* | node() | Contacts/Contact/Note"/></xsl:copy></xsl:模板></xsl:stylesheet>

I've done a lot of searching, but I cannot figure out how to use templates exactly.

My input data is called 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>

My XSL called Test.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Indentation in XSL -->
<xsl:output omit-xml-declaration="yes" 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="Contact">
        <xsl:copy>
                <!--
                Apply the attributes of the current node and the attributes of all
                childs
                -->
                <xsl:apply-templates select="@* | child::node()[not(self::Note)]"/>
        </xsl:copy>
    <xsl:apply-templates select="Note"/>
</xsl:template>

</xsl:stylesheet>

Wanted output:

<?xml version="1.0" ?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="eExact-Schema.xsd">
<Accounts>
 <Account code="                 868" status="A" type="C">
  <Name>Name</Name>
    <Contacts>
   <Contact default="1" gender="M" status="A">
    <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>
  <Note>Patient: 1</Note>
    <Debtor number="   1" code="                 1">
   <Currency code="EUR"/>
   </Debtor>
   </Account>
</Accounts>
</eExact>

My problem is that with my XSL the node "Note" comes as a child of contact, but i want it as a child of account. Hope someone will help me?

解决方案

My problem is that with my XSL the node "Note" comes as a child of contact, but i want it as a child of account.

Well, then you need to exclude it from Contact, and include it in Account:

XSLT 1.0

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

<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" indent="yes"/>

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

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

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

</xsl:stylesheet>

这篇关于使用 XSLT 向上移动节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆