没有,基本上是这样。你应该,但是,能够使用这样的:
<xsl:key name="ids" match="*[@z:Id]" use="@z:Id"/>
,然后使用XSL key
函数传递当前节点,其中z
是xmlns
别名http://schemas.microsoft.com/2003/10/Serialization/
的@z:Ref
- 将至少保持用法始终如一。
完整的示例 - XSLT第一( “my.xslt”):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
xmlns:dcs="http://schemas.datacontract.org/2004/07/"
>
<xsl:key name="ids" match="*[@z:Id]" use="@z:Id"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*[@z:Ref]">
<xsl:param name="depth" select="5"/>
<xsl:apply-templates select="key('ids', @z:Ref)">
<xsl:with-param name="depth" select="$depth"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*[@z:Id]">
<xsl:param name="depth" select="5"/>
<xsl:value-of select="$depth"/>: <xsl:value-of select="name()"/><xsl:text xml:space="preserve">
</xsl:text>
<xsl:if test="$depth > 0">
<xsl:apply-templates select="dcs:*">
<xsl:with-param name="depth" select="($depth)-1"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
注意,这经由$depth
参数(递减)行走5级;关键部分是任何元素*[@z:Ref]
上的初始match
,然后使用key
将相同请求代理到原始元素,通过@z:Id
解析。这意味着,当我们正在向子元素,我们只需要使用类似:
<xsl:apply-templates select="dcs:*"/>
虽然我们可以明显更为精细,例如:
<xsl:apply-templates select="dcs:Foo"/>
还要注意添加Foo
特异性match
,你可以这样:
<xsl:template match="dcs:Foo[@z:Id]"><!-- --></xsl:template>
,以确保我们的*[@z:Ref]
比赛继续处理参考forwardi NG。
和C#:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
[DataContract]
public class Foo
{
[DataMember]
public Bar Bar { get; set; }
}
[DataContract]
public class Bar
{
[DataMember]
public Foo Foo { get; set; }
}
static class Program
{
static void Main()
{
var foo = new Foo();
var bar = new Bar();
foo.Bar = bar;
bar.Foo = foo;
using (var ms = new MemoryStream())
{
var ser = new DataContractSerializer(typeof(Foo), new DataContractSerializerSettings {
PreserveObjectReferences = true
});
ser.WriteObject(ms, foo);
Console.WriteLine(Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length));
Console.WriteLine();
ms.Position = 0;
var xslt = new XslCompiledTransform();
xslt.Load("my.xslt");
using (var reader = XmlReader.Create(ms))
{
xslt.Transform(reader, null, Console.Out);
}
}
Console.WriteLine();
Console.WriteLine("press any key");
Console.ReadKey();
}
}
我无法理解你的观点?你能否用另一种方式解释它? – anilca
@anilca“不,你不能让它递归一点 - 但你至少可以很容易地在对象之间跳转,通过使用xsl -key” –
如何到达使用引用的实体xsl键? – anilca