【发布时间】:2013-09-20 13:15:28
【问题描述】:
我正在尝试将所有 <type> 值更新为小写,现在工作正常。此外,我想将命名空间从 v2.0 更改为 v1.0,它工作得很好,只是我不想为下面的“category”和“authList”等子元素声明命名空间。我想做的最重要的事情是用<Number> 替换<Name> 元素以及它的新数值。
所以我想在这里实现的两件杰出的事情是:
1. 使用新的数值将“名称”元素更新为“数字”。 (对我来说必须)
2. 如果可能,从“类别”或文档中除根元素之外的其他子元素中删除命名空间。 (如果可能)
请让我知道我的 XSLT 出了什么问题。非常感谢。我希望我能够保持问题简单。
XML 输入:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<ns6:QueryResponse version="2.0" xmlns="" xmlns:ns6="http://www.abc.com/s/v2.0">
<category>
<categoryList>
<cat>
<type>SUPER</type>
<value>gg44</value>
</cat>
<cat>
<type>SUPER2</type>
<value>fff</value>
</cat>
</categoryList>
</category>
<AuthList>
<sAuthority>
<Name>P</Name>
</sAuthority>
<AuthList>
</ns6:QueryResponse>
</soapenv:Body>
</soapenv:Envelope>
XML 输出 -
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<v1:QueryResponse version="1.0" xmlns:v1="http://www.abc.com/s/v1.0">
<category xmlns:ns2="http://www.abc.com/s/v1.0" xmlns:ns3="http://www.abc.com/s/v2.0">
<categoryList>
<cat>
<type>super</type>
<value>gg44</value>
</cat>
<cat>
<type>super2</type>
<value>fff</value>
</cat>
</categoryList>
</category>
<AuthList xmlns:ns2="http://www.abc.com/s/v1.0" xmlns:ns3="http://www.abc.com/s/v2.0">
<sAuthority>
<Name>P</Name>
</sAuthority>
<AuthList>
</v1:QueryResponse>
</soapenv:Body>
</soapenv:Envelope>
XSLT 代码:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http://www.abc.com/s/v2.0"
exclude-result-prefixes="old"
xmlns:v1="http://www.abc.com/s/v1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"
version="1.0" />
<!-- Some parameters declered -->
<xsl:param name="newversion" select="'1.0'" />
<xsl:param name="P_1" select="'1'" />
<xsl:param name="C_2" select="'2'" />
<xsl:param name="S_3" select="'3'" />
<xsl:param name="F_4" select="'4'" />
<!-- This is to update namespace from v2.0 to v1.0 -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="old:*">
<xsl:element name="v1:{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="old:QueryResponse/@version">
<xsl:attribute name="version">
<xsl:value-of select="$newversion" />
</xsl:attribute>
</xsl:template>
<!-- This is to update the Name to Number -->
<xsl:template match="sAuthority/Name">
<xsl:choose>
<xsl:when test=".='P'">
<xsl:element name="Number">
<xsl:value-of select="$P_1"/>
</xsl:element>
</xsl:when>
<xsl:when test=".='C'">
<xsl:element name="Number">
<xsl:value-of select="$C_2"/>
</xsl:element>
</xsl:when>
<xsl:when test=".='S'">
<xsl:element name="Number">
<xsl:value-of select="$S_3"/>
</xsl:element>
</xsl:when>
<xsl:when test=".='F'">
<xsl:element name="Number">
<xsl:value-of select="$F_4"/>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- This is to update Upper case values to lower case for all the type elements in the XML input whereever they are in the XML -->
<xsl:template match="type/text()">
<xsl:value-of
select="translate (., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
</xsl:template>
</xsl:stylesheet>
预期输出:-
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<v1:QueryResponse version="1.0" xmlns:v1="http://www.abc.com/s/v1.0">
<category>
<categoryList>
<cat>
<type>super</type>
<value>gg44</value>
</cat>
<cat>
<type>super2</type>
<value>fff</value>
</cat>
</categoryList>
</category>
<AuthList>
<sAuthority>
<Number>1</Number>
</sAuthority>
<AuthList>
</v1:QueryResponse>
</soapenv:Body>
</soapenv:Envelope>
谢谢,
【问题讨论】:
-
您说要将值转换为小写,但您的预期输出值是大写的。您说您想从(我只能假设)
QueryResponse中的所有内容中删除名称空间,但是您的输入在QueryResponse中的所有内容上都没有名称空间。这里似乎有很多不一致之处。 -
抱歉,我刚刚更新了帖子。如果我在这里有意义,请告诉我......并提出解决方法。同样是的,我的输入 XML 没有子元素的命名空间,它仅在根元素 QueryResponse 上。
-
在这种情况下,XML 输出(您的第二个代码 sn-p)的意义是什么?那是你目前得到的输出吗?看起来您的输入在
QueryResponse内的任何内容上仍然没有命名空间。您是否不小心删除了它们? -
是的,XML 输出是我在应用上面共享的 XSLT 代码后得到的。我在实际输入文件中的任何地方都没有命名空间,除了根元素。原始文件太大,我刚刚减少了数据元素以发布问题。
-
我没有看到与 QueryResponse 的任何子元素关联的任何命名空间。我们错过了什么吗?那么,你想用命名空间做什么?如果它不存在,那么您将如何删除它? ;) 另外,在 XSLT 中,只需检查 xpath 中的元素 Number。
标签: xslt