0

我是 xslt 的新手并试图创建一个 XSLT ,我正在调用另一个模板并将参数值传递给它但是不知何故,目标模板的参数变量中出现了空白值。

这是简化的示例 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

<?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=""

预期结果是:它应该更新我从模板 1 传递的 owning_organization 属性,当它的 @object_name="'Test'" 时,UGPART 的 value = $RefPrimaryObjectId

这里出了什么问题?为什么 $RefPrimaryObjectId" 值没有出现在第二个模板中。

4

1 回答 1

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>

如果我误解了您的预期输出,请告诉我们。

于 2012-06-15T08:14:18.733 回答