xml - 使用 XSLT 向上移动节点

标签 xml 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 名为 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>

想要输出:

<?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>

我的问题是,对于我的 XSL,节点“Note”作为联系人的子节点,但我希望它作为帐户的子节点。希望有人能帮助我吗?

最佳答案

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.

那么您需要将其从联系人排除,并在帐户包含:

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>

关于xml - 使用 XSLT 向上移动节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26121407/

相关文章:

xml - 选择一个带有 xmlns 的节点?

c# - c#中xlsx中xml的文件路径

javascript - 从本地文件发出 XML 跨域请求

c# - XmlNode.InnerText

java - XmlPullParser 名称预期异常

python - 使用 Scrapy 生成 XML 页面

xslt - XSLT 2.0-如何将'('和')'替换为字符串中的空白?

xml - XSLT 传递参数

c# - 在 C# 中读取 XML 字符串

xml - 根据节点值对XPath查询进行分组?