XSLT 中的排除结果前缀不起作用
exclude-result-prefixes in XSLT is not working
我有以下 xml:
<?xml version="1.0" encoding="UTF-8"?>
<comments xmlns="http://www.dsttechnologies.com/awd/rest/v1">
<row xmlns="">
<source>SYSTEM</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">Assigned to: DT73606</text>
<date>2015-08-18</date>
<posted>8 days ago</posted>
<time>04:04:58-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName xmlns="http://www.dsttechnologies.com/awd/rest/v1">All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/b5ee129a-0338-41c7-881b-ab8c1727de36" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
<row xmlns="">
<source>MANUAL</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">hai hello</text>
<date>2015-07-29</date>
<posted>3 weeks ago</posted>
<time>09:04:25-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName xmlns="http://www.dsttechnologies.com/awd/rest/v1">All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/6c09f55e-f06f-4fa1-abe4-4bc4eed14e6d" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
</comments>
我正在尝试使用 XSLT 修改上面的 xml,输出如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<History>
<comments>
<row>
<source>SYSTEM</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">Assigned to: DT73606</text>
<date>2015-08-18</date>
<posted>8 days ago</posted>
<time>04:04:58-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName>All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/b5ee129a-0338-41c7-881b-ab8c1727de36" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
<row>
<source>MANUAL</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">hai hello</text>
<date>2015-07-29</date>
<posted>3 weeks ago</posted>
<time>09:04:25-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName>All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/6c09f55e-f06f-4fa1-abe4-4bc4eed14e6d" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
</comments>
</History>
最终目标是消除命名空间。我正在尝试使用以下 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="http://www.dsttechnologies.com/awd/rest/v1" version="2.0" exclude-result-prefixes="v">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="v:comments">
<History>
<xsl:copy>
<xsl:apply-templates select="v:comment" />
</xsl:copy>
</History>
</xsl:template>
<xsl:template match="v:comment">
<row>
<xsl:apply-templates mode="sans-namespace" />
</row>
</xsl:template>
<xsl:template match="*" mode="sans-namespace">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
尽管使用 exclude-result-prefixes="v"
,但我没有得到想要的输出。有人可以指出这里有什么问题吗?
更改模板:
<xsl:template match="v:comments">
<History>
<xsl:copy>
<xsl:apply-templates select="v:comment" />
</xsl:copy>
</History>
</xsl:template>
到
<xsl:template match="v:comments">
<History>
<comments>
<xsl:apply-templates select="@*|node()" />
</comments>
</History>
</xsl:template>
应该适用于您的情况。
但是,XSLT 2.0 在 <xsl:copy>
指令中提供了 copy-namespaces
和 inherit-namespaces
,但它们似乎不起作用。
当您复制一个元素时,您也复制了它的命名空间。这与exclude-result-prefixes
无关:实际使用的名称空间永远不会被排除。
在你的例子中,你可以简单地做:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v="http://www.dsttechnologies.com/awd/rest/v1"
exclude-result-prefixes="v">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" 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="/v:comments">
<History>
<comments>
<xsl:apply-templates/>
</comments>
</History>
</xsl:template>
</xsl:stylesheet>
我有以下 xml:
<?xml version="1.0" encoding="UTF-8"?>
<comments xmlns="http://www.dsttechnologies.com/awd/rest/v1">
<row xmlns="">
<source>SYSTEM</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">Assigned to: DT73606</text>
<date>2015-08-18</date>
<posted>8 days ago</posted>
<time>04:04:58-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName xmlns="http://www.dsttechnologies.com/awd/rest/v1">All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/b5ee129a-0338-41c7-881b-ab8c1727de36" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
<row xmlns="">
<source>MANUAL</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">hai hello</text>
<date>2015-07-29</date>
<posted>3 weeks ago</posted>
<time>09:04:25-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName xmlns="http://www.dsttechnologies.com/awd/rest/v1">All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/6c09f55e-f06f-4fa1-abe4-4bc4eed14e6d" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
</comments>
我正在尝试使用 XSLT 修改上面的 xml,输出如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<History>
<comments>
<row>
<source>SYSTEM</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">Assigned to: DT73606</text>
<date>2015-08-18</date>
<posted>8 days ago</posted>
<time>04:04:58-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName>All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/b5ee129a-0338-41c7-881b-ab8c1727de36" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
<row>
<source>MANUAL</source>
<group>PRTL-TASK</group>
<type>text/plain</type>
<text mimetype="text/plain">hai hello</text>
<date>2015-07-29</date>
<posted>3 weeks ago</posted>
<time>09:04:25-05:00</time>
<userNameInfo href="awdServer/awd/services/v1/users/DT73606" userId="DT73606" id="1e1fd97d-2d14-4939-a009-5c94fdb7b754">
<lastName>All powerful UserId</lastName>
</userNameInfo>
<instanceId>2015-07-29-04.27.15.461040T01</instanceId>
<link rel="self" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01/history/comments/6c09f55e-f06f-4fa1-abe4-4bc4eed14e6d" />
<link rel="parent instance" type="application/vnd.dsttechnologies.awd+xml" href="awdServer/awd/services/v1/instances/2015-07-29-04.27.15.461040T01" />
</row>
</comments>
</History>
最终目标是消除命名空间。我正在尝试使用以下 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:v="http://www.dsttechnologies.com/awd/rest/v1" version="2.0" exclude-result-prefixes="v">
<xsl:strip-space elements="*" />
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="v:comments">
<History>
<xsl:copy>
<xsl:apply-templates select="v:comment" />
</xsl:copy>
</History>
</xsl:template>
<xsl:template match="v:comment">
<row>
<xsl:apply-templates mode="sans-namespace" />
</row>
</xsl:template>
<xsl:template match="*" mode="sans-namespace">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
尽管使用 exclude-result-prefixes="v"
,但我没有得到想要的输出。有人可以指出这里有什么问题吗?
更改模板:
<xsl:template match="v:comments">
<History>
<xsl:copy>
<xsl:apply-templates select="v:comment" />
</xsl:copy>
</History>
</xsl:template>
到
<xsl:template match="v:comments">
<History>
<comments>
<xsl:apply-templates select="@*|node()" />
</comments>
</History>
</xsl:template>
应该适用于您的情况。
但是,XSLT 2.0 在 <xsl:copy>
指令中提供了 copy-namespaces
和 inherit-namespaces
,但它们似乎不起作用。
当您复制一个元素时,您也复制了它的命名空间。这与exclude-result-prefixes
无关:实际使用的名称空间永远不会被排除。
在你的例子中,你可以简单地做:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v="http://www.dsttechnologies.com/awd/rest/v1"
exclude-result-prefixes="v">
<xsl:output method="xml" version="1.0" omit-xml-declaration="yes" encoding="UTF-8" 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="/v:comments">
<History>
<comments>
<xsl:apply-templates/>
</comments>
</History>
</xsl:template>
</xsl:stylesheet>