2013-08-23 54 views
1

我试图增加新的元素,并使用模板复制并使用一个XSLT,但我无法看到结果,我归因于两家母公司及其子元素的子元素期待。新元素/属性添加到Parent和使用单XSLT

我很新的XSLT,并在多个模板仅仅是匹配的根元素正在工作的模板。其他与该子元素匹配的模板不起作用。

任何帮助和技术将是很有益的。

原始XML

<?xml version="1.0" encoding="UTF-8"?> 
<Order> 
    <OrderLines Type="Text"> 
     <OrderLine Type="Fine Print"> 

     </OrderLine> 
    </OrderLines> 
</Order> 

预计XML XSLT转换

<?xml version="1.0" encoding="UTF-8"?> 
<Order> 
    <Instructions> 
     <Instruction InstructionType="Valid" InstructionText="See me" /> 
    </Instructions> 
    <OrderLines Type="Text" Value="9"> 
     <OrderLine Type="Fine Print" Value="3"> 

     </OrderLine> 
    </OrderLines> 
</Order> 

回答

0
<?xml version="1.0" encoding="UTF-16"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0"> 
    <xsl:output omit-xml-declaration="yes" method="xml" version="1.0" /> 
    <xsl:template match="/"> 
    <xsl:apply-templates select="/Order" /> 
    </xsl:template> 
    <xsl:template match="/Order"> 
    <Order> 
     <Instructions> 
     <Instruction> 
      <xsl:attribute name="InstructionType"> 
      <xsl:text>Valid</xsl:text> 
      </xsl:attribute> 
      <xsl:attribute name="InstructionText"> 
      <xsl:text>See me</xsl:text> 
      </xsl:attribute> 
     </Instruction> 
     </Instructions> 
     <OrderLines> 
     <xsl:attribute name="Type"> 
      <xsl:value-of select="OrderLines/@Type" /> 
     </xsl:attribute> 
     <xsl:attribute name="Value"> 
      <xsl:value-of select="OrderLines/@Value" /> 
     </xsl:attribute> 
     <OrderLine> 
      <xsl:attribute name="Type"> 
      <xsl:value-of select="OrderLines/OrderLine/@Type" /> 
      </xsl:attribute> 
      <xsl:attribute name="Value"> 
      <xsl:value-of select="OrderLines/OrderLine/@Value" /> 
      </xsl:attribute> 
     </OrderLine> 
     </OrderLines> 
    </Order> 
    </xsl:template> 
</xsl:stylesheet> 
+0

我不认为这会工作,因为在输入不存在'Value'属性。此外,你应该看看使用AVTS为未来的样式表:http://www.w3.org/TR/xslt#attribute-value-templates –

0

我会做的是先从identity transform,然后overrid根据需要。 (这通常被称为 “推” 式。)

实施例...

XML输入

<Order> 
    <OrderLines Type="Text"> 
     <OrderLine Type="Fine Print"> 

     </OrderLine> 
    </OrderLines> 
</Order> 

XSLT 1.0

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

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

    <xsl:template match="Order"> 
     <xsl:copy> 
      <Instructions> 
       <Instruction InstructionType="Valid" InstructionText="See me" /> 
      </Instructions> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="OrderLines"> 
     <OrderLines Value="9"> 
      <xsl:apply-templates select="@*|node()"/> 
     </OrderLines> 
    </xsl:template> 

    <xsl:template match="OrderLine"> 
     <OrderLine Value="3"> 
      <xsl:apply-templates select="@*|node()"/> 
     </OrderLine> 
    </xsl:template> 

</xsl:stylesheet> 

XML输出

<Order> 
    <Instructions> 
     <Instruction InstructionType="Valid" InstructionText="See me"/> 
    </Instructions> 
    <OrderLines Value="9" Type="Text"> 
     <OrderLine Value="3" Type="Fine Print"/> 
    </OrderLines> 
</Order>