使用单个XSLT向Parent及其子元素添加新元素/属性



我正在尝试使用模板复制和一个XSLT向Parent及其子元素添加新元素和属性,但无法看到预期的结果。

我是XSLT的新手,在多个模板中,只有与根元素匹配的模板才能工作。与子元素匹配的其他模板不起作用。

任何帮助和技巧都会非常有用。

原始XML

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

XSLT转换后的预期XML

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

我要做的是从身份转换开始,然后根据需要覆盖它。(这通常被称为"推"式。)

示例。。。

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>

最新更新