[英]XSLT code for xml transformation
我想使用xslt進行xml轉換。 這是需要轉換的xml:
之所以失敗,是因為缺少屬性值“ xsi:type”的命名空間,即“ Insert”,我希望將其命名為“ ns:Insert”。 我嘗試使用在線xsl轉換器( http://www.utilities-online.info/xsltransformation/#.WrtA4S5uZEQ ),它在那里工作正常,但是一旦將其放入代碼中即可。 我沒有得到想要的前綴。 任何原因嗎?
我必須將xmlns:v1="http://stil.dk/ipung/services/synclokationer/v1.0"
到您的源XML中,否則格式不正確。
我使用了以下腳本(略微修改了您的版本):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1="http://stil.dk/ipung/services/synclokationer/v1.0"
xmlns:ns="http://www.logica.com/veu/syncSkole/dto/Lokationer"
exclude-result-prefixes="v1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:com="http://www.logica.com/veu/syncSkole/dto/common">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:element name="{local-name()}"><xsl:apply-templates select="@* | node()"/></xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:template>
<xsl:template match="comment() | text() | processing-instruction()">
<xsl:copy />
</xsl:template>
<xsl:template match="v1:Lokation">
<xsl:element name="{local-name()}">
<xsl:attribute name="xsi:type">
<xsl:value-of select="translate(@xsi:type, 'v1', 'ns')" />
</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:element>
</xsl:template>
<xsl:template match="v1:Modtager | v1:ModtagerSystemID |
v1:ModtagerSystemTransaktionsID | v1:Afsendelsestidspunkt |
v1:BeskedID | v1:InstNr">
<xsl:element name="{local-name()}"
namespace="http://www.logica.com/veu/syncSkole/dto/common">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<!-- Caution: Namespace different than above -->
<xsl:template match="v1:syncLokationer">
<xsl:element name="ns:{local-name()}"
namespace="http://www.logica.com/veu/syncSkole/dto/Lokationer">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在www.utilities-online.info/xsltransformation下,我得到了Lokation
元素,如下所示:
<Lokation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="ns:Insert">
因此xsi:type
屬性的值為ns:Insert
,正如您所期望的那樣。
正如Parfait提議的那樣,我修改了腳本以消除重復的模板,僅在match屬性上有所不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.