【发布时间】:2018-02-08 06:39:50
【问题描述】:
我需要将 xml 中的所有属性转换为具有某些条件的元素。例如,某些属性应该以“值”为前缀。我做到了这一点。除此之外,我还需要更改我的命名空间。我做不到。
XML
<Template xmlns="styling/1.0.0" Name="TemplateFromDictionary">
<Style Name="Default">
<Fill Color=""/>
<Stroke Color="0000FF" LineStyle="Single" Width="1"/>
<Symbol Color="FFFFFF" Name="default.png" ScaleX="100" ScaleY="100" ScaleMode="Drawing"/>
</Style>
<Style Name="Parcel">
<Fill Color="48F5F5F5"/>
<Stroke Color="C0C0C0" LineStyle="Single" Width="1"/>
<Symbol Color="FFFFFF" Name="default.png" ScaleX="100" ScaleY="100" ScaleMode="Drawing"/>
</Style>
</Template>
XSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
xmlns:s="styling/1.0.0"
xmlns="styling/1.0.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="s:Style|s:Template">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:variable name="name">
<xsl:apply-templates select="." mode="name"/>
</xsl:variable>
<xsl:element name="{$name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@Color|@Width|@ScaleX|@ScaleY|@LeftIndent|@RightIndent|@FirstLineIndent|@SpaceBefore|@SpaceAfter|@Size" mode="name">
<xsl:value-of select="concat(name(), 'Value')"/>
</xsl:template>
<xsl:template match="@*" mode="name">
<xsl:value-of select="name()"/>
</xsl:template>
</xsl:stylesheet>
输出
<?xml version="1.0" encoding="utf-8"?>
<Template Name="TemplateFromDictionary" xmlns="styling/1.0.0">
<Style Name="Default">
<Fill>
<ColorValue></ColorValue>
</Fill>
<Stroke>
<ColorValue>0000FF</ColorValue>
<LineStyle>Single</LineStyle>
<WidthValue>1</WidthValue>
</Stroke>
<Symbol>
<ColorValue>FFFFFF</ColorValue>
<Name>default.png</Name>
<ScaleXValue>100</ScaleXValue>
<ScaleYValue>100</ScaleYValue>
<ScaleMode>Drawing</ScaleMode>
</Symbol>
</Style>
<Style Name="Parcel">
<Fill>
<ColorValue>48F5F5F5</ColorValue>
</Fill>
<Stroke>
<ColorValue>C0C0C0</ColorValue>
<LineStyle>Single</LineStyle>
<WidthValue>1</WidthValue>
</Stroke>
<Symbol>
<ColorValue>FFFFFF</ColorValue>
<Name>default.png</Name>
<ScaleXValue>100</ScaleXValue>
<ScaleYValue>100</ScaleYValue>
<ScaleMode>Drawing</ScaleMode>
</Symbol>
</Style>
</Template>
在输出而不是这个
<Template Name="TemplateFromDictionary" xmlns="styling/1.0.0">
我需要这个
<Template Name="TemplateFromDictionary" xmlns="styling/2.0.0">
我尝试将 xslt 中的命名空间更改为 xmlns="styling/2.0.0" 但这给出的结果类似于
<Fill><ColorValue xmlns="styling/2.0.0"></ColorValue></Fill>
命名空间嵌入到所有元素中,模板元素看起来相同
<Template Name="TemplateFromDictionary" xmlns="styling/1.0.0">
我需要与上面提到的输出完全相同的输出,只是需要更改模板元素中的命名空间。
我正在用 C# 转换它。
请帮帮我。
【问题讨论】: