我有一個需要重命名元素的 xml 檔案。(后來也將元素轉換為屬性)。xslt 轉換將默認命名空間插入到所有頂級元素中。我不想要那個。我已經看到了很多關于這個問題的問題,但仍然對期望的結果有問題并理解為什么......
- 我能用 exclude-result-prefixes 做的最好的事情是:
<ar xmlns="">
<dyu xmlns="http://www.coastsystems.net">ábada</dyu>
為什么命名空間只從 ar 中洗掉?為什么它會保留 xmlns=""?
- 如果我洗掉轉換的重命名部分,那么它基本上只是復制原始檔案:
<xsl:template match="/c:lexique/c:headword">
<ar>
<xsl:apply-templates select="@* | node()"/>
</ar>
</xsl:template>
那么'exclude-result-prefixes'似乎根本沒有效果。無論它是否存在,都沒有插入命名空間。為什么?
xslt3 有問題嗎?我需要使用它,因為它可以處理拉丁擴展字符并正確排序。
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 headwords2.xsd"
>
<headword>
<dyu>ábada</dyu>
<alt>abada</alt>
<emp></emp>
<cf>fewu</cf>
<trans>
<lang>fr</lang>
<detail></detail>
<speech>
<type></type>
<t-uuid>7b8612bc-23c7-4241-817f-f6fcd9bff8ac</t-uuid>
<def>
<gloss>jamais</gloss>
<gl-id>bbc05aae-8f08-4ab7-91ae-3b52533a896f</gl-id>
<note2></note2>
<note3></note3>
<note4></note4>
<tags></tags>
<example>
<source>a t? ko?uman k? abada.</source>
<target>Il ne fait jamais quelque chose de bien.</target>
<ex-id>2c592b68-0d29-4b6c-8614-005adab4fba5</ex-id>
</example>
</def>
</speech>
</trans>
</headword>
</lexique>
使用 xslt 樣式表:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns:c="http://www.coastsystems.net"
exclude-result-prefixes="xsl c"
>
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/c:lexique/c:headword">
<ar>
<xsl:apply-templates select="@* | node()"/>
</ar>
</xsl:template>
</xsl:stylesheet>
(xslt3 -xsl:rename.xsl -s:headwords.xml)輸出:
<?xml version="1.0" encoding="UTF-8"?>
<lexique xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.coastsystems.net" xsi:schemaLocation="http://www.coastsystems.net headwords2.xsd">
<ar xmlns="">
<dyu xmlns="http://www.coastsystems.net">ábada</dyu>
<alt xmlns="http://www.coastsystems.net">abada</alt>
<emp xmlns="http://www.coastsystems.net"/>
<cf xmlns="http://www.coastsystems.net">fewu</cf>
<trans xmlns="http://www.coastsystems.net">
<lang>fr</lang>
<detail/>
<speech>
<type/>
<t-uuid>7b8612bc-23c7-4241-817f-f6fcd9bff8ac</t-uuid>
<def>
<gloss>jamais</gloss>
<gl-id>bbc05aae-8f08-4ab7-91ae-3b52533a896f</gl-id>
<note2/>
<note3/>
<note4/>
<tags/>
<example>
<source>a t? ko?uman k? abada.</source>
<target>Il ne fait jamais quelque chose de bien.</target>
<ex-id>2c592b68-0d29-4b6c-8614-005adab4fba5</ex-id>
</example>
</def>
</speech>
</trans>
</ar>
期望的輸出:
<?xml version="1.0" encoding="UTF-8"?>
<lexique xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.coastsystems.net" xsi:schemaLocation="http://www.coastsystems.net headwords2.xsd">
<ar>
<dyu>ábada</dyu>
<alt>abada</alt>
<emp/>
<cf>fewu</cf>
<trans>
<lang>fr</lang>
<detail/>
===== snipped
uj5u.com熱心網友回復:
exclude-result-prefixes僅在不使用它們時才排除它們的指令。如果您希望在沒有命名空間的情況下生成這些其他元素,那么您需要創建沒有命名空間的元素(使用local-name()),而不是使用xsl:copy.
您可以通過為任何元素添加通用模板來實作這一點(遵循匹配的身份模板,@*|node()使其具有更高的優先級匹配。
如果您希望所有元素都系結到 `` 命名空間,但不使用前綴,則在樣式表中設定默認命名空間xmlns="http://www.coastsystems.net":
<xsl:stylesheet version="2.0"
xmlns:xsl = "http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.coastsystems.net"
xmlns:c="http://www.coastsystems.net"
exclude-result-prefixes="xsl c"
>
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="/c:lexique/c:headword">
<ar>
<xsl:apply-templates select="@* | node()"/>
</ar>
</xsl:template>
</xsl:stylesheet>
您還可以更改身份模板的匹配運算式以列出特定node()而不包括*: @*|text()|comment()|processing-instruction()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/476584.html
上一篇:mysql動態搜索查詢索引
下一篇:從字串中獲取特定單詞
