0

我正在尝试生成几个文本文件,具体取决于输入 XML 文档中存在的对象元素的数量。为此,我使用result-document()(xslt 2.0) 函数,并由 Saxon 评估转换。

出于某种原因,在 Saxon 中运行转换时,我没有从某些xsl:value-of函数中获取值,但是当我在 VS 2010 Ultimate 中运行标准 XSLT 调试(我需要注释掉xsl:result-document函数)时,我得到了预期的结果。问题是植根于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 代码:代码文件

这是代表预期和实际转换结果的示例:

实际结果:

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);
        }
4

1 回答 1

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);
}

所以我无法重现这个问题。当结果缺少这些值时,您确定输入样本中存在您尝试输出的属性吗?

于 2013-06-22T16:40:45.140 回答