为自定义域排序 xsl 条 xsi:schemaLocation
Sort xsl strips xsi:schemaLocation for custom domain
我需要对 xml 文件进行排序,但是当我 运行 转换时,它会从 header 中删除 xsi:schemaLocation。奇怪的是,如果我将命名空间 url 更改为 www.example.com,它不会被删除。我真的很困惑。
所以 xsltproc sort.xsl test.xml
将 return 这个:
<?xml version="1.0"?>
<lexique xmlns="http://www.coastsystems.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<headword>
<dyu>à</dyu>
但是如果我将 url 更改为 example.com,相同的转换将 return:
<?xml version="1.0"?>
<lexique xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com headwords.xsd">
<headword>
<dyu>bôn</dyu>
test.xml:
?xml version='1.0' encoding='utf-8'?>
<lexique xmlns="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.coastsystems.net headwords.xsd"
>
<headword>
<dyu>bôn</dyu>
<alt></alt>
<trans>
<lang>fr</lang>
<detail></detail>
<speech>
<type></type>
<def>
<gloss>lancer</gloss>
<note></note>
<example>
<source></source>
<target></target>
</example>
</def>
</speech>
</trans>
</headword>
<headword>
<dyu>à</dyu>
<alt></alt>
<trans>
<lang>fr</lang>
<detail></detail>
<speech>
<type>pr 3è s.</type>
<def>
<gloss>il / elle</gloss>
<note></note>
<example>
<source>Musa k'a dɔgɔcɛ bugɔ.</source>
<target>Moussa a frappé son frère.</target>
</example>
</def>
</speech>
</trans>
</headword>
</lexique>
sort.xsl
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="z:lexique">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="z:dyu" lang="en" data-type="text" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
headwords.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.coastsystems.net" xmlns:xs="http://www.w
<xs:element name="lexique" type="coas:lexiqueType" xmlns:coas="http://www.coastsystems.net"/>
<xs:complexType name="exampleType">
<xs:sequence>
<xs:element type="xs:string" name="source"/>
<xs:element type="xs:string" name="target"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="defType">
<xs:sequence>
<xs:element type="xs:string" name="gloss"/>
<xs:element type="xs:string" name="note">
</xs:element>
<xs:element type="coas:exampleType" name="example" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="speechType">
<xs:sequence>
<xs:element type="xs:string" name="type"/>
<xs:element type="coas:defType" name="def" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="transType">
<xs:sequence>
<xs:element type="xs:string" name="lang"/>
<xs:element type="xs:string" name="detail">
</xs:element>
<xs:element type="coas:speechType" name="speech" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="headwordType">
<xs:sequence>
<xs:element type="xs:string" name="dyu"/>
<xs:element type="xs:string" name="alt" maxOccurs="unbounded" minOccurs="0">
</xs:element>
<xs:element type="coas:transType" name="trans" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="lexiqueType">
<xs:sequence>
<xs:element type="coas:headwordType" name="headword" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xsl:copy>
只复制元素,不复制属性。
所以你也必须复制根节点的属性。例如:<xsl:copy-of select="@*"/>
这个 xslt:
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="z:lexique">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates>
<xsl:sort select="z:dyu" lang="en" data-type="text" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
给出这个结果:
<?xml version="1.0" encoding="UTF-8"?>
<lexique xmlns="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.coastsystems.net headwords.xsd">
<headword>
<dyu>à</dyu>
<alt/>
<trans>
<lang>fr</lang>
<detail/>
<speech>
<type>pr 3è s.</type>
<def>
<gloss>il / elle</gloss>
<note/>
<example>
<source>Musa k'a dɔgɔcɛ bugɔ.</source>
<target>Moussa a frappé son frère.</target>
</example>
</def>
</speech>
</trans>
</headword>
<headword>
<dyu>bôn</dyu>
<alt/>
<trans>
<lang>fr</lang>
<detail/>
<speech>
<type/>
<def>
<gloss>lancer</gloss>
<note/>
<example>
<source/>
<target/>
</example>
</def>
</speech>
</trans>
</headword>
</lexique>
我需要对 xml 文件进行排序,但是当我 运行 转换时,它会从 header 中删除 xsi:schemaLocation。奇怪的是,如果我将命名空间 url 更改为 www.example.com,它不会被删除。我真的很困惑。
所以 xsltproc sort.xsl test.xml
将 return 这个:
<?xml version="1.0"?>
<lexique xmlns="http://www.coastsystems.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<headword>
<dyu>à</dyu>
但是如果我将 url 更改为 example.com,相同的转换将 return:
<?xml version="1.0"?>
<lexique xmlns="http://www.example.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.com headwords.xsd">
<headword>
<dyu>bôn</dyu>
test.xml:
?xml version='1.0' encoding='utf-8'?>
<lexique xmlns="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.coastsystems.net headwords.xsd"
>
<headword>
<dyu>bôn</dyu>
<alt></alt>
<trans>
<lang>fr</lang>
<detail></detail>
<speech>
<type></type>
<def>
<gloss>lancer</gloss>
<note></note>
<example>
<source></source>
<target></target>
</example>
</def>
</speech>
</trans>
</headword>
<headword>
<dyu>à</dyu>
<alt></alt>
<trans>
<lang>fr</lang>
<detail></detail>
<speech>
<type>pr 3è s.</type>
<def>
<gloss>il / elle</gloss>
<note></note>
<example>
<source>Musa k'a dɔgɔcɛ bugɔ.</source>
<target>Moussa a frappé son frère.</target>
</example>
</def>
</speech>
</trans>
</headword>
</lexique>
sort.xsl
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="z:lexique">
<xsl:copy>
<xsl:apply-templates>
<xsl:sort select="z:dyu" lang="en" data-type="text" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
headwords.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.coastsystems.net" xmlns:xs="http://www.w
<xs:element name="lexique" type="coas:lexiqueType" xmlns:coas="http://www.coastsystems.net"/>
<xs:complexType name="exampleType">
<xs:sequence>
<xs:element type="xs:string" name="source"/>
<xs:element type="xs:string" name="target"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="defType">
<xs:sequence>
<xs:element type="xs:string" name="gloss"/>
<xs:element type="xs:string" name="note">
</xs:element>
<xs:element type="coas:exampleType" name="example" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="speechType">
<xs:sequence>
<xs:element type="xs:string" name="type"/>
<xs:element type="coas:defType" name="def" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="transType">
<xs:sequence>
<xs:element type="xs:string" name="lang"/>
<xs:element type="xs:string" name="detail">
</xs:element>
<xs:element type="coas:speechType" name="speech" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="headwordType">
<xs:sequence>
<xs:element type="xs:string" name="dyu"/>
<xs:element type="xs:string" name="alt" maxOccurs="unbounded" minOccurs="0">
</xs:element>
<xs:element type="coas:transType" name="trans" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="lexiqueType">
<xs:sequence>
<xs:element type="coas:headwordType" name="headword" maxOccurs="unbounded" minOccurs="0" xmlns:coas="http://www.coastsystems.net"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xsl:copy>
只复制元素,不复制属性。
所以你也必须复制根节点的属性。例如:<xsl:copy-of select="@*"/>
这个 xslt:
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="z:lexique">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates>
<xsl:sort select="z:dyu" lang="en" data-type="text" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
给出这个结果:
<?xml version="1.0" encoding="UTF-8"?>
<lexique xmlns="http://www.coastsystems.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.coastsystems.net headwords.xsd">
<headword>
<dyu>à</dyu>
<alt/>
<trans>
<lang>fr</lang>
<detail/>
<speech>
<type>pr 3è s.</type>
<def>
<gloss>il / elle</gloss>
<note/>
<example>
<source>Musa k'a dɔgɔcɛ bugɔ.</source>
<target>Moussa a frappé son frère.</target>
</example>
</def>
</speech>
</trans>
</headword>
<headword>
<dyu>bôn</dyu>
<alt/>
<trans>
<lang>fr</lang>
<detail/>
<speech>
<type/>
<def>
<gloss>lancer</gloss>
<note/>
<example>
<source/>
<target/>
</example>
</def>
</speech>
</trans>
</headword>
</lexique>