XSLT 1.0:搜索属性并添加到父元素(第2部分)

时间:2015-07-01 01:14:34

标签: xslt-1.0

使用XSLT 1.0我需要转换它:

<wine id="_7" grape="chardonnay"> 
    <product>Carneros</product>
    <year>1997</year>
    <price>10.99</price>
    <Method context="_5" attributes="gccxml(msgid=237)">
        <Argument location="f0:13"/>
    </Method>
    <Field id="_3" context="_7" attributes="gccxml(msgid=239)"/>
    <Field id="_4" line="19" attributes=""/>
    <Method context="_8" attributes="">
        <Argument location="f0:13"/>
    </Method>
</wine>
  
      
  • 进入:

         

    XML输出-1:

  •   
<wine grape="chardonnay" msgid="239">
   <product>Carneros</product>
   <year>1997</year>
   <price>10.99</price>
   <Method context="_5" attributes="gccxml(msgid=237)">
        <Argument location="f0:13"/>
    </Method>
    <Field id="3" context="_7" attributes="gccxml(msgid=239)"/>
    <Field id="4" line="19"/>
    <Method context="_8">
        <Argument location="f0:13"/>
    </Method>
</wine>

如何完成'XML Output-1'的逻辑如下:     ================================================== ==========      - 逻辑1:     ----------     搜索匹配葡萄酒[@id]的'Field [@context]'或'Method [@context]'节点。

For instance: 
<wine id="_7" grape="chardonnay"> 
<Field id="_3" context="_7" attributes="gccxml(msgid=239)"/>

To conclude:
The wine[id="_7"] matches Field[context="_7"], so add the attributes="gccxml(msgid=239)"
in 'wine' element (i.e. msgid="239")

Result:
<wine grape="chardonnay" msgid="239">

- Logic 2:
----------
If 'Field' or 'Method' nodes includes attributes="" (null attribute),
then delete the attributes="" in output.

For Instance:
1. <Field id="4" line="19"/>
2. <Method context="_8">
  

另外,我需要转移这个:

XML Input-2

 <wine id="_5" grape="chardonnay"> 
        <product>Carneros</product>
        <year>1997</year>
        <price>10.99</price>
        <Field id="_9" line="19" attributes=""/>
        <Method context="_5" attributes="gccxml(msgid=235)">
            <Argument location="f0:13"/>
        </Method>
        <Field id="_3" context="_7" attributes="gccxml(msgid=239)"/>
        <Field id="_4" line="19" attributes=""/>
    </wine>
  

进入这个

XML输出-2:

<wine grape="chardonnay" msgid="235">
   <product>Carneros</product>
   <year>1997</year>
   <price>10.99</price>
   <Field id="_9" line="19"/>
   <Method context="_5" attributes="gccxml(msgid=235)">
        <Argument location="f0:13"/>
    </Method>
    <Field id="3" context="_7" attributes="gccxml(msgid=239)"/>
    <Field id="4" line="19"/>
</wine>   

1 个答案:

答案 0 :(得分:0)

  

搜索&#39; Field [@context]&#39;或者&#39;方法[@context]&#39;节点   匹配葡萄酒[@id]。

使用 key - XSLT的内置机制来处理交叉引用很容易。一旦完成,它只是重新运行your previous question

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="msg" match="Field|Method" use="@context"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/*">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:attribute name="msgid">
            <xsl:value-of select="substring-before(substring-after(key('msg', @id)/@attributes, 'msgid='), ')')"/>      
        </xsl:attribute>
        <xsl:apply-templates select="node()"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

加了:

要删除FieldMethod elemmnts的空属性,只需添加一个与之匹配的空模板:

<xsl:template match="@*[parent::Field or parent::Method][not (string())]"/>