我的目標是能夠:
xmlns="http://xmlns.example.com/v1"用xmlns="http://xmlns.example.com"(不帶 v1)替換根元素- 將根元素替換
schemaVersion="1"為schemaVersion="2"(版本 2) - 應用其他模板匹配以排除(而不是添加)我需要從輸入 xml 中洗掉的節點。
輸入xml
<account xmlns="http://xmlns.example.com/v1"
xmlns:ns2="http://xmlns.example/v2"
schemaVersion="1">
<head>Jane Doe</head>
<accountNumber>1234567</accountNumber>
<messageType>CREATE</messageType>
<create>
<ns2:profile>
<ns2:accountNumber>1234567</ns2:accountNumber>
</ns2:profile>
</create>
<ns2:tax>
<ns2:taxExempt>false</ns2:taxExempt>
</ns2:tax>
<contact>
<ns2:address>US</ns2:address>
</contact>
</account>
所需的輸出 xml
<account xmlns="http://xmlns.example.com"
xmlns:ns2="http://xmlns.example/v2"
schemaVersion="2">
<head>Jane Doe</head>
<messageType>ADD</messageType>
<action>A</action>
<ns2:profile>
<ns2:accountNumber>1234567</ns2:accountNumber>
</ns2:profile>
<contact>
<ns2:address>US</ns2:address>
</contact>
</account>
在所需的輸出中,我
- 將根元素第一個命名空間值從更改
xmlns="http://xmlns.example.com/v1"為xmlns="http://xmlns.example.com"(不帶 v1) - 將根元素的
schemaVersion屬性值從更改schemaVersion="1"為schemaVersion="2"(版本 2) - 已移除
<accountNumber> <messageType>將值從CREATE更改為ADD- 添加
<action>A</action> - 洗掉父節點
<create>但保留其子節點。 - 洗掉了整個
<ns2:tax>節點及其子節點
我設法產生了所需的輸出。但是,它引入了一個問題。
問題:在當前輸出 xml 中,xmlns="http://xmlns.example.com/v1" (版本 1)被添加到<head>和<contact>.
xmlns="http://xmlns.example.com/v1" 應該走開。
我的 xsl 的當前輸出
<account xmlns="http://xmlns.example.com"
xmlns:ns2="http://xmlns.example/v2"
schemaVersion="2">
<head xmlns="http://xmlns.example.com/v1">Jane Doe</head>
<messageType>ADD</messageType>
<action>A</action>
<ns2:profile>
<ns2:accountNumber>1234567</ns2:accountNumber>
</ns2:profile>
<contact xmlns="http://xmlns.example.com/v1">
<ns2:address>US</ns2:address>
</contact>
</account>
我的 XSL 代碼
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://xmlns.example.com/v1"
xmlns:ns2="http://xmlns.example/v2">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*" />
<!-- Copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- Replace root's xmlns and schemaVersion value -->
<xsl:template match="account">
<account xmlns="http://xmlns.example.com"
xmlns:ns2="http://xmlns.example/v2"
schemaVersion="2">
<xsl:apply-templates />
</account>
</xsl:template>
<!-- remove <accountNumber> -->
<xsl:template match="accountNumber" />
<!-- Change messageType value from CREATE to ADD -->
<!-- Add <action>A</action> -->
<xsl:template
match="account/messageType[text()='CREATE']">
<xsl:element name='messageType'
namespace='http://xmlns.example.com'>
<xsl:value-of select="'ADD'" />
</xsl:element>
<xsl:element name='action'
namespace='http://xmlns.example.com'>
<xsl:value-of select="'A'" />
</xsl:element>
</xsl:template>
<!-- remove node <create> but keep its children -->
<xsl:template match="create">
<xsl:apply-templates />
</xsl:template>
<!-- Removed entire <ns2:tax> node and its children -->
<xsl:template match="ns2:tax" />
<!-- Ensure that the namespace of nodes prefixed with ns2 has "http://xmlns.example/v2" value -->
<xsl:template match="ns2:*">
<xsl:element name="ns2:{local-name()}"
namespace="http://xmlns.example/v2">
<xsl:apply-templates select="@*, node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我遇到了困難,因為我必須首先設定xpath-default-namespace="http://xmlns.example.com/v1"為版本 1 以匹配我要洗掉的節點,然后我最終需要將 xmlns 值替換為非版本 1 ( xmlns="http://xmlns.example.com/")
我將不勝感激有關解決此問題的任何建議或想法。
謝謝你。
uj5u.com熱心網友回復:
如果您想要或需要更改元素的名稱空間(并且在某個元素上宣告的名稱空間適用于其所有后代,因此您需要更改根及其所有后代的名稱空間),則單獨的身份轉換無濟于事;基于此,我認為您需要撰寫一個模板作為更改元素名稱空間的起點:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://xmlns.example.com"
xmlns:v1="http://xmlns.example.com/v1"
xpath-default-namespace="http://xmlns.example.com/v1"
xmlns:ns2="http://xmlns.example/v2"
exclude-result-prefixes="v1">
<xsl:output method="xml" encoding="utf-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/*/@schemaVersion">
<xsl:attribute name="{name()}">2</xsl:attribute>
</xsl:template>
<!-- Change namespace http://xmlns.example.com/v1 to http://xmlns.example.com-->
<xsl:template match="v1:*">
<xsl:element name="{local-name()}">
<xsl:copy-of select="namespace::*[not(. = 'http://xmlns.example.com/v1')]"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<!-- remove <accountNumber> -->
<xsl:template match="accountNumber" />
<!-- Change messageType value from CREATE to ADD -->
<!-- Add <action>A</action> -->
<xsl:template
match="account/mesageType[.='CREATE']">
<messageType>ADD</messageType>
<action>A</action>
</xsl:template>
<!-- remove node <create> but keep its children -->
<xsl:template match="create">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="ns2:*">
<xsl:copy copy-namespaces="no">
<xsl:copy-of select="namespace::*[not(. = 'http://xmlns.example.com/v1')]"/>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Removed entire <ns2:tax> node and its children -->
<xsl:template match="ns2:tax" />
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471875.html
