0

This is my new (edited) more complete scenario: I got a xml, that will transform with xsl. I got an example from the customer, that looks like: XML that is infile:

<?xml version="1.0" encoding="utf-8"?>
<xsi:myCompanyImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tns:myCompanyHeader xmlns:tns="http://www.MyComp.se/myCompany">
    <tns:sentFr>123456</tns:sentFr>
  </tns:myCompanyHeader>
  <tns:myCompanyUsers xmlns:tns="http://www.MyComp.se/myCompany">
    <tns:myCompanyUser ssn="1234567890">
      <tns:firstName>John</tns:firstName>
    </tns:myCompanyUser>
  </tns:myCompanyUsers>
</xsi:myCompanyImport>

XSL-file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:tns="http://www.MyComp.se/myCompany"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://www.MyComp.se/myCompany myCompanyUsers.xsd"
                                xmlns:local="urn:cs">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
        <xsl:element name="xsi:myCompanyImport" namespace="http://www.w3.org/2001/XMLSchema-instance">
            <!--Start Header-->
            <xsl:element name="tns:myCompanyHeader" >
                <xsl:element name="tns:sentFr" >
                    <xsl:value-of select="_x0023_MyComp_header/@SentFrom" />
                </xsl:element>
            </xsl:element>
            <!--End Header-->

            <!--Start Users-->
            <xsl:element name="tns:myCompanyUsers">
                <!--Loop through persons-->
                <xsl:for-each select="_x0023_MyComp_header/_x0023_MyComp_Employee">
                    <xsl:element name="tns:myCompanyUser" >
                        <xsl:attribute name="ssn">
                            <xsl:value-of select="@inSsn" />
                        </xsl:attribute>
                        <xsl:element name="tns:firstName" >
                            <xsl:value-of select="@inFname"/>
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
                <!--End Loop-->
            </xsl:element>
            <!--End Users-->

        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

(Nevermind about the Csharp-code in the xsl, it's used for purposes not shown here.) So, the result is now:

<?xml version="1.0" encoding="utf-8"?>
<xsi:myCompanyImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tns:myCompanyHeader xmlns:tns="http://www.MyComp.se/myCompany">
    <tns:sentFr>123456</tns:sentFr>
  </tns:myCompanyHeader>
  <tns:myCompanyUsers xmlns:tns="http://www.MyComp.se/myCompany">
    <tns:myCompanyUser ssn="1234567890">
      <tns:firstName>John</tns:firstName>
    </tns:myCompanyUser>
  </tns:myCompanyUsers>
</xsi:myCompanyImport>

But the customer wants:

<?xml version="1.0" encoding="utf-8"?>
<xsi:myCompanyImport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <tns:myCompanyHeader xmlns:tns="http://www.MyComp.se/myCompany">
    <tns:sentFr>123456</tns:sentFr>
  </tns:myCompanyHeader>
  <tns:myCompanyUsers xmlns:tns="http://www.MyComp.se/myCompany"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.maxm.se/myCompany myCompanyUsers.xsd">
    <tns:myCompanyUser ssn="1234567890">
      <tns:firstName>John</tns:firstName>
    </tns:myCompanyUser>
  </tns:myCompanyUsers>
</xsi:myCompanyImport>

How can I write this in the xsl so that shows up in the xml? I've tried many different ways, but with no success. It's actually just those 2 lines I would add to tns:myCompanyUsers:

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.maxm.se/myCompany myCompanyUsers.xsd">

I want in. But no success!

4

1 回答 1

2

如何在 xsl 中编写它以便显示在 xml 中?

按字面意思写,即代替

<xsl:element name="myns:myCustomerUsers" >

利用

<myns:myCustomerUsers xmlns:tns="http://www.myCustomerWeb.se/myCustomer" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.myCustomerWeb.se/myCustomer myexuser.xsd">

或者简单地说

<myns:myCustomerUsers 
    xsi:schemaLocation="http://www.myCustomerWeb.se/myCustomer myexuser.xsd">

因为文字结果元素继承了样式表中当时范围内的命名空间节点(在您的情况下,那些在 上声明的节点<xsl:stylesheet>)。您可能需要添加exclude-result-prefixes="msxsl"到您的<xsl:stylesheet>元素中,否则该myns:myCustomerUsers元素也将获得xmlns:msxsl="urn:schemas-microsoft-com:xslt"声明。

<xsl:element>仅当元素名称不是常量时才需要使用。

于 2013-06-18T15:28:03.487 回答