我是xslt的新手并尝试创建一个XSLT, 我正在调用另一个模板,将param值传递给它 但不知何故,空白值出现在目标模板的参数变量中。
以下是简化的示例xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<TCXML xmlns="http://www.tcxml.org/Schemas/TCXMLSchema">
<BOMWindow elemId="id62" revision_rule="id60" top_line="id2">
<GSIdentity elemId="id1" label="a00pC7EtM1CZ7D"/>
</BOMWindow>
<IMAN_Drawing elemId="id78" owning_site="id4" primary_object="#id21" secondary_object="#id25" user_data="">
<GSIdentity elemId="id50" label="R8D9sTvgBT4jNA"/>
</IMAN_Drawing>
<IMAN_Drawing elemId="id79" owning_site="#id4" primary_object="#id22" secondary_object="#id26" user_data="">
<GSIdentity elemId="id56" label="R8D9sTfVBT4jNA"/>
</IMAN_Drawing>
<IMAN_Drawing elemId="id80" owning_site="#id4" primary_object="#id20" secondary_object="#id24" user_data="">
<GSIdentity elemId="id44" label="R8A9sTu6BT4jNA"/>
</IMAN_Drawing>
<UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" object_name="Test" owning_organization="" >
<GSIdentity elemId="id21" label="RXM9sPifBT4jNA"/>
</UGPART>
<UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" object_name="Test2" owning_organization="" >
<GSIdentity elemId="id20" label="R3K9sPifBT4jNA"/>
</UGPART>
<UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" object_name="Test3" owning_organization="" >
<GSIdentity elemId="id22" label="xuO9sPifBT4jNA"/>
</UGPART>
在XML中有3个IMAN_Drawing标记和3个UGPART标记
这是我的xslt for this
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:plm="http://www.tcxml.org/Schemas/TCXMLSchema" version="1.0">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/plm:TCXML/plm:IMAN_Drawing">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:for-each select="/plm:TCXML/plm:IMAN_Drawing">
<xsl:variable name="currentSecObjectId" select="@secondary_object"/>
<xsl:variable name="RefSecObjectId" select="substring($currentSecObjectId,2)"/>
<xsl:variable name="currentPrimaryObjectId" select="/plm:TCXML/plm:IMAN_Drawing/@primary_object"/>
<xsl:variable name="RefPrimaryObjectId" select="substring($currentPrimaryObjectId,2)"/>
<xsl:call-template name="UGPart">
<xsl:with-param name="PriId" select="$RefPrimaryObjectId"/>
</xsl:call-template>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="/plm:TCXML/plm:UGPART" name="UGPart">
<xsl:param name="PriId"/>
<xsl:copy>
<xsl:if test="@object_name="'Test'">
<xsl:attribute name="owning_organization">
<xsl:value-of select="$PriId"/>
</xsl:attribute>
</xsl:if>
<xsl:copy-of select="@*[not(name()='owning_organization')]" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但输出是: owning_organization = “”
预期结果是: 当@object_name =“'Test'”
时,它应该更新我从模板1传递的owning_organization属性,其值为= $ RefPrimaryObjectId for UGPART这里出了什么问题?为什么$ RefPrimaryObjectId“值不会进入第二个模板。
答案 0 :(得分:0)
你应该真的提供你的预期输出,否则很难理解你想要的东西。我根据您提供的样式表对您的预期输出进行了一些猜测。
我的意思是没有不尊重,我只是想提供帮助,但你的样式表指出了很多误解,而不是仅仅选择我的解决方案(下面提供),从长远来看,你可能会更好,放下笔,阅读一些已发表的教程或阅读一本书。
任何方式......
此样式表......
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:plm="http://www.tcxml.org/Schemas/TCXMLSchema" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="plm:IMAN_Drawing">
<xsl:variable name="id" select="substring(@primary_object,2)" />
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
<xsl:for-each select="../plm:UGPART[plm:GSIdentity/@elemId=$id]" >
<xsl:copy>
<xsl:apply-templates select="@*[name()!='owning_organization' or ../@object_name!='Test']"/>
<xsl:if test="@object_name='Test'">
<xsl:attribute name="owning_organization"><xsl:value-of select="$id" /></xsl:attribute>
</xsl:if>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:for-each>
</xsl:template >
<xsl:template match="plm:UGPART" />
</xsl:stylesheet>
...当应用于您的样本输入时,将生成输出...
<?xml version="1.0" encoding="utf-8"?>
<TCXML xmlns="http://www.tcxml.org/Schemas/TCXMLSchema">
<BOMWindow elemId="id62" revision_rule="#id60" top_line="#id2">
<GSIdentity elemId="id1" label="a00pC7EtM1CZ7D" />
</BOMWindow>
<IMAN_Drawing elemId="id78" owning_site="#id4" primary_object="#id21" secondary_object="#id25" user_data="" /><UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" object_name="Test" owning_organization="id21">
<GSIdentity elemId="id21" label="RXM9sPifBT4jNA" />
</UGPART>
<IMAN_Drawing elemId="id79" owning_site="#id4" primary_object="#id22" secondary_object="#id26" user_data="" /><UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" object_name="Test3" owning_organization="">
<GSIdentity elemId="id22" label="xuO9sPifBT4jNA" />
</UGPART>
<IMAN_Drawing elemId="id80" owning_site="#id4" primary_object="#id20" secondary_object="#id24" user_data="" /><UGPART creation_date="2012-06-05T09:25:30Z" date_released="2012-06-05T09:25:33Z" ead_paragraph="" elemId="id117" format_used="PART" gov_classification="" object_name="Test2" owning_organization="">
<GSIdentity elemId="id20" label="R3K9sPifBT4jNA" />
</UGPART>
</TCXML>
如果我误解了您的预期输出,请告诉我们。