当根元素具有默认的命名空间属性而不是它时,我在xslt行为中遇到了一个特殊的区别.
我想知道为什么会出现这种差异.
XML输入是
<root>
<content>xxx</content>
</root>
Run Code Online (Sandbox Code Playgroud)
应用以下转换时
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>
<xsl:template match="content">
<w>x</w>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
结果是预期的
<?xml version="1.0" encoding="UTF-8"?>
<root>
<w>x</w>
</root>
Run Code Online (Sandbox Code Playgroud)
但是当应用相同的转换时
<root xmlns="http://test.com">
<content>xxx</content>
</root>
Run Code Online (Sandbox Code Playgroud)
结果是不同的,并且基于默认模板的应用(有效输出文本节点值'xxx'):
<?xml version="1.0" encoding="UTF-8"?>
<root>xxx</root>
Run Code Online (Sandbox Code Playgroud)
加成
如果这是这种情况下的预期行为,那么content在第二种情况下需要匹配元素的匹配属性值是什么?
我正在设置一个SOAP Web服务,它应该返回一个复合消息.
此消息的有效实例如下:
<dl190Response xmlns="http://pse/">
<cdhead cisprik="5563167"/>
<mvts>
<mvts_S att="a1">
<x>x1</x>
<w>w1</w>
</mvts_S>
<mvts_S>
<x>x2</x>
<w>w2</w>
</mvts_S>
</mvts>
</dl190Response>
Run Code Online (Sandbox Code Playgroud)
所有这些都在wsdl中得到了很好的定义:
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://pse/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
name="PSE"
targetNamespace="http://pse/">
<types>
<xs:schema xmlns="http://pse/" targetNamespace="http://pse/">
<xs:complexType name="cdhead_T">
<xs:attribute name="cisprik" type="xs:long"/>
</xs:complexType>
<xs:complexType name="mvts_T">
<xs:sequence>
<xs:element name="mvts_S" type="mvts_S_T" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="mvts_S_T">
<xs:sequence>
<xs:element name="x" type="xs:string"/>
<xs:element name="w" type="xs:string"/>
</xs:sequence>
<xs:attribute name="att" type="xs:string" use="optional"/>
</xs:complexType>
</xs:schema>
</types>
<message name="DL190Req">
<part name="cdhead" type="tns:cdhead_T"/>
</message>
<message name="DL190Res">
<part name="cdhead" type="tns:cdhead_T"/>
<part name="mvts" …Run Code Online (Sandbox Code Playgroud) 我试图将java函数转换为xsl:function规范.该函数基本上将html标记放在子字符串周围.我现在遇到了困难:使用java内联代码这很有效,但我无法弄清楚在使用xsl:function时如何防止输出转义.如何实现输出以包含所需的html标记?
我想要实现的一个简化示例如下:输入参数值"AB"应该导致字符串A <b> B </ b>,在html浏览器中显示为A B当然.
我试过的示例功能如下; 但随后得到的字符串是A< b> B</b> (请注意,我必须添加空格以防止实体在此编辑器中被解释),这当然会在浏览器中显示为A <b> B </ b>.
请注意,xsl:element不能在xsl:function代码中使用,因为它没有任何效果; 我希望函数调用的字符串结果包含<和>字符,然后将字符串结果添加到输出结果文件.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:custom="http://localhost:8080/customFunctions">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
<xsl:function name="custom:test">
<xsl:param name="str"/>
<xsl:value-of select="substring($str,1,1)"/>
<xsl:text disable-output-escaping="yes"><![CDATA[<b>]]></xsl:text>
<xsl:value-of select="substring($str,2)"/>
<xsl:text disable-output-escaping="yes"><![CDATA[</b>]]></xsl:text>
</xsl:function>
<xsl:template match="/">
<xsl:element name="html">
<xsl:element name="body">
<xsl:value-of select="custom:test('AB')"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud) 我有一个类型的对象
com.sun.org.apache.xerces.internal.dom.DeferredElementImpl
Run Code Online (Sandbox Code Playgroud)
包含XML根元素和结构.
如何从这个字符串中检索XML内容?
请注意,该方法toString()是可用的,但这已经以非常基本的方式实现,仅报告字符串[root: null],因此仅显示根元素名称而不显示此元素的任何其他内容.
在javadoc中,此方法被列为"用于调试方便的非DOM方法".
xml ×2
xslt ×2
dom ×1
function ×1
html ×1
namespaces ×1
php ×1
soap ×1
soapserver ×1
web-services ×1
xpath ×1
xslt-2.0 ×1