Change Node name of an XSLT based on an include fi

2019-07-31 05:15发布

Hi I have a XSLT file which need to transform an XML file.

Sample XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <Document>
      <Customer>
        <Header>
          <Message>
            <xsl:value-of select="//Header/Message"/>
          </Message>
       </Header>
     </Customer>
    </Document>
  </xsl:Template>
  <xsl:include href="Inc1.xsl" />
</xsl:stylesheet> 

Now what I need is to change the node name Customer to Supplier depending on the include filename at the bottom and one more thing, I have specific attributes for the Document node also depending on the include file.

Thanks and hope someone can help me out.

标签: xml xslt
1条回答
狗以群分
2楼-- · 2019-07-31 05:26

The only viable mechanism is to have the included stylesheet handle the specific element names and <Document> attributes: Something like this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <Document>
            <xsl:call-template name="apply-document-attributes" />
            <xsl:element name="{$customer-element-name}">
                <Header>
                    <Message>
                        <xsl:value-of select="//Header/Message" />
                    </Message>
                </Header>
            </xsl:element>
        </Document>
    </xsl:template>

    <xsl:include href="Inc1.xsl" />

</xsl:stylesheet> 

and the included stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="customer-element-name" select="'Supplier'"/>

    <xsl:template name="apply-document-attributes">
        <xsl:attribute name="foo">bar</xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

or,

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="customer-element-name" select="'Customer'"/>

    <xsl:template name="apply-document-attributes"/>
</xsl:stylesheet>
查看更多
登录 后发表回答