我正在尝试生成多个文本文件,具体取决于输入XML文档中存在的对象元素的数量。为此,我使用result-document()
(xslt 2.0)函数,并且Saxon评估转换。
出于某种原因,在Saxon中运行转换时,我没有从某些xsl:value-of
函数获取值,但是当我运行标准XSLT调试(我需要注释掉xsl:result-document
函数)时VS 2010 Ultimate,我得到了预期的结果。问题根植于 FillTheObjectFromForm 模板xsl:for-each
循环。
有人可以指出我的模板中有什么问题吗?
为了简化这篇文章,我已将XML和结果文件上传到http://hotfile.com。以下是代码示例和输入XML:
XML:
<?xml version="1.0" encoding="utf-8"?>
<dbs:MetaDataRoot FreeForm="true" xmlns:dbs="http://stefan/DbStructure">
<orm:MappingRoot FreeForm="False" MapDataStructure="ASPBaza" TransactionType="ADONET" Namespace="GeneratedCode" xmlns:orm="http://stefan/ORM.xsd">
<orm:Build MapDataStructure="ASPBaza" />
<orm:Assembly Namespace="GeneratedCode" MapDataStructure="ASPBaza" Name="BusinessLayer">
<orm:Objects>
<orm:Object OriginalName="Kupac" Name="Kupac" TableName="Kupac" CollectionName="Kupac" Caption="Kupac" >
<orm:Properties>
<orm:Property Name="SifraKupca" IsPrimaryKey="true" ControlType="System.Windows.Forms.TextBox" ControlPrefix="txt" ControlName="txtSifraKupca" BindProperty="Text" Display="false"></orm:Property>
<orm:Property Name="Ime" IsPrimaryKey="false" ControlPrefix="txt" ControlName="txtIme" BindProperty="Text" Display="true"></orm:Property>
<orm:Property Name="Prezime" IsPrimaryKey="false" ControlPrefix="txt" ControlName="txtPrezime" BindProperty="Text" Display="true"></orm:Property>
<orm:Property Name="BrojTelefona" IsPrimaryKey="false" ControlPrefix="txt" ControlName="txtBrojTelefona" BindProperty="Text" Display="true"></orm:Property>
<orm:Property Name="Adresa" IsPrimaryKey="false" ControlPrefix="txt" ControlName="txtAdresa" BindProperty="Text" Display="true"></orm:Property>
</orm:Properties>
<orm:AllProperties />
</orm:Object>
...
</orm:Objects>
</orm:Assembly>
</orm:MappingRoot>
</dbs:MetaDataRoot>
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
xmlns:orm="http://stefan/ORM.xsd"
xmlns:ui="http://stefan/UserInterface">
<xsl:output method="text" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates
select="//orm:Assembly//orm:Objects"
mode="Object"/>
</xsl:copy>
</xsl:template>
<xsl:template match="orm:Objects" mode="Object">
<xsl:for-each select="orm:Object">
<xsl:variable name="dirname"
select ="'..\..\UI\FinalResultWebUIDesignClasses\'"/>
<xsl:variable name="filename"
select="concat($dirname,@Name,'Edit','.aspx.cs')"/>
<xsl:result-document method="text" href="{$filename}">
<xsl:call-template name ="SaveButtonEvent"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template name="SaveButtonEvent">
protected void SaveButtonEvent(object sender, EventArgs e)
{
MiddletierManager mm = new MiddletierManager();
<xsl:value-of select="@Name"/> objectClass = new <xsl:value-of select="@Name"/>();
<xsl:call-template name="FillTheObjectFromForm"/>
mm.Save(objectClass);
}
</xsl:template>
<xsl:template name="FillTheObjectFromForm">
<xsl:for-each select="orm:Properties/*">
objectClass.<xsl:value-of select="@Name"/> = this.<xsl:choose>
<xsl:when test ="@ControlPrefix = 'txt' and @IsPrimaryKey='false'">
<xsl:value-of select="@ControlName"/>.<xsl:value-of select="@BindProperty"/>;
</xsl:when>
<xsl:when test ="@ControlPrefix = 'txt' and @IsPrimaryKey='true'">hdnID.Value;</xsl:when>
<xsl:when test="@ControlPrefix ='cbo'">
<xsl:value-of select="@ControlName"/>.<xsl:value-of select="@BindProperty"/>;
</xsl:when>
<xsl:when test="@ControlPrefix ='dtp'">
<xsl:value-of select="@ControlName"/>.Value;
</xsl:when>
<xsl:when test="@ControlPrefix ='chk'">
<xsl:value-of select="@ControlName"/>.Checked;
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
用于解析XSLT的SAXON代码:code file
以下是表示预期和实际转化结果的示例:
实际结果:
protected void SaveButtonEvent(object sender, EventArgs e)
{
MiddletierManager mm = new MiddletierManager();
Korpa objectClass = new Korpa();
objectClass.KorpaID = this.
objectClass.SifraKupca = this.
objectClass.Datum = this.
mm.Save(objectClass);
}
预期结果:
protected void SaveButtonEvent(object sender, EventArgs e)
{
MiddletierManager mm = new MiddletierManager();
Korpa objectClass = new Korpa();
objectClass.KorpaID = this.hdnID.Value;
objectClass.SifraKupca = this.SelectedValue;
objectClass.Datum = this.dtpDatum.Value;
mm.Save(objectClass);
}
答案 0 :(得分:0)
我已经运行了你的样式表(只编辑了文件名分配)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="msxsl"
xmlns:orm="http://stefan/ORM.xsd"
xmlns:ui="http://stefan/UserInterface">
<xsl:output method="text" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates
select="//orm:Assembly//orm:Objects"
mode="Object"/>
</xsl:copy>
</xsl:template>
<xsl:template match="orm:Objects" mode="Object">
<xsl:for-each select="orm:Object">
<xsl:variable name="dirname"
select ="'..\..\UI\FinalResultWebUIDesignClasses\'"/>
<xsl:variable name="filename"
select="concat(@Name,'Edit','.aspx.cs')"/>
<xsl:result-document method="text" href="{$filename}">
<xsl:call-template name ="SaveButtonEvent"/>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template name="SaveButtonEvent">
protected void SaveButtonEvent(object sender, EventArgs e)
{
MiddletierManager mm = new MiddletierManager();
<xsl:value-of select="@Name"/> objectClass = new <xsl:value-of select="@Name"/>();
<xsl:call-template name="FillTheObjectFromForm"/>
mm.Save(objectClass);
}
</xsl:template>
<xsl:template name="FillTheObjectFromForm">
<xsl:for-each select="orm:Properties/*">
objectClass.<xsl:value-of select="@Name"/> = this.<xsl:choose>
<xsl:when test ="@ControlPrefix = 'txt' and @IsPrimaryKey='false'">
<xsl:value-of select="@ControlName"/>.<xsl:value-of select="@BindProperty"/>;
</xsl:when>
<xsl:when test ="@ControlPrefix = 'txt' and @IsPrimaryKey='true'">hdnID.Value;</xsl:when>
<xsl:when test="@ControlPrefix ='cbo'">
<xsl:value-of select="@ControlName"/>.<xsl:value-of select="@BindProperty"/>;
</xsl:when>
<xsl:when test="@ControlPrefix ='dtp'">
<xsl:value-of select="@ControlName"/>.Value;
</xsl:when>
<xsl:when test="@ControlPrefix ='chk'">
<xsl:value-of select="@ControlName"/>.Checked;
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
使用Saxon 9.5 HE Java对照您在帖子中显示的输入,结果为KupacEdit.aspx.cs
,内容为
protected void SaveButtonEvent(object sender, EventArgs e)
{
MiddletierManager mm = new MiddletierManager();
Kupac objectClass = new Kupac();
objectClass.SifraKupca = this.hdnID.Value;
objectClass.Ime = this.txtIme.Text;
objectClass.Prezime = this.txtPrezime.Text;
objectClass.BrojTelefona = this.txtBrojTelefona.Text;
objectClass.Adresa = this.txtAdresa.Text;
mm.Save(objectClass);
}
所以我无法重现这个问题。当结果缺少这些值时,您确定输入样本中是否存在您尝试输出的属性吗?