Xml XSLT变量不充当节点集
我有一个XML,其中一些原始值显示为nodeset。我想将其作为变量,并在进一步的过程中使用。请查看以下内容: XML:Xml XSLT变量不充当节点集,xml,xslt,Xml,Xslt,我有一个XML,其中一些原始值显示为nodeset。我想将其作为变量,并在进一步的过程中使用。请查看以下内容: XML: <?xml version="1.0" encoding="UTF-8"?> <root> <element1>Some value in the element1</element1> <element2 value="<sno>1</sno>&am
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element1>Some value in the element1</element1>
<element2 value="<sno>1</sno><name>Amrendra</name><mobile>0123456789</mobile>"></element2>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="element">
<root>
<xsl:value-of select="/root/element2/@value" disable-output-escaping="yes"/>
</root>
</xsl:variable>
<xsl:template match="/">
<root>
<applytemp>
<xsl:apply-templates select="$element/root/name"/>
</applytemp>
<copy-of>
<xsl:copy-of select="$element/root/name"/>
</copy-of>
<value-of>
<xsl:value-of select="$element/root/name"/>
</value-of>
</root>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp/>
<copy-of/>
<value-of/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp>Amrendra</applytemp>
<copy-of>Amrendra</copy-of>
<value-of>Amrendra</value-of>
</root>
元素1中的某些值
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element1>Some value in the element1</element1>
<element2 value="<sno>1</sno><name>Amrendra</name><mobile>0123456789</mobile>"></element2>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="element">
<root>
<xsl:value-of select="/root/element2/@value" disable-output-escaping="yes"/>
</root>
</xsl:variable>
<xsl:template match="/">
<root>
<applytemp>
<xsl:apply-templates select="$element/root/name"/>
</applytemp>
<copy-of>
<xsl:copy-of select="$element/root/name"/>
</copy-of>
<value-of>
<xsl:value-of select="$element/root/name"/>
</value-of>
</root>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp/>
<copy-of/>
<value-of/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp>Amrendra</applytemp>
<copy-of>Amrendra</copy-of>
<value-of>Amrendra</value-of>
</root>
电流输出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element1>Some value in the element1</element1>
<element2 value="<sno>1</sno><name>Amrendra</name><mobile>0123456789</mobile>"></element2>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="element">
<root>
<xsl:value-of select="/root/element2/@value" disable-output-escaping="yes"/>
</root>
</xsl:variable>
<xsl:template match="/">
<root>
<applytemp>
<xsl:apply-templates select="$element/root/name"/>
</applytemp>
<copy-of>
<xsl:copy-of select="$element/root/name"/>
</copy-of>
<value-of>
<xsl:value-of select="$element/root/name"/>
</value-of>
</root>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp/>
<copy-of/>
<value-of/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp>Amrendra</applytemp>
<copy-of>Amrendra</copy-of>
<value-of>Amrendra</value-of>
</root>
必须退出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element1>Some value in the element1</element1>
<element2 value="<sno>1</sno><name>Amrendra</name><mobile>0123456789</mobile>"></element2>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="element">
<root>
<xsl:value-of select="/root/element2/@value" disable-output-escaping="yes"/>
</root>
</xsl:variable>
<xsl:template match="/">
<root>
<applytemp>
<xsl:apply-templates select="$element/root/name"/>
</applytemp>
<copy-of>
<xsl:copy-of select="$element/root/name"/>
</copy-of>
<value-of>
<xsl:value-of select="$element/root/name"/>
</value-of>
</root>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp/>
<copy-of/>
<value-of/>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp>Amrendra</applytemp>
<copy-of>Amrendra</copy-of>
<value-of>Amrendra</value-of>
</root>
阿姆伦德拉
阿姆伦德拉
阿姆伦德拉
如下更改变量声明
<xsl:variable name="element">
<root>
<xsl:analyze-string select="//root/element2/@value" regex="<([a-z]+)>(.*?)</\1>">
<xsl:matching-substring>
<xsl:element name="{regex-group(1)}"><xsl:value-of select="regex-group(2)"/></xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
</root>
</xsl:variable>
输出
<root>
<applytemp>
<name>Amrendra</name>
</applytemp>
<copy-of>
<name>Amrendra</name>
</copy-of>
<value-of>Amrendra</value-of>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp>
<name>Amrendra</name>
</applytemp>
<copy-of>
<name>Amrendra</name>
</copy-of>
<value-of>Amrendra</value-of>
</root>
如下更改变量声明
<xsl:variable name="element">
<root>
<xsl:analyze-string select="//root/element2/@value" regex="<([a-z]+)>(.*?)</\1>">
<xsl:matching-substring>
<xsl:element name="{regex-group(1)}"><xsl:value-of select="regex-group(2)"/></xsl:element>
</xsl:matching-substring>
</xsl:analyze-string>
</root>
</xsl:variable>
输出
<root>
<applytemp>
<name>Amrendra</name>
</applytemp>
<copy-of>
<name>Amrendra</name>
</copy-of>
<value-of>Amrendra</value-of>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root>
<applytemp>
<name>Amrendra</name>
</applytemp>
<copy-of>
<name>Amrendra</name>
</copy-of>
<value-of>Amrendra</value-of>
</root>
我有一个XML,其中一些原始值显示为nodeset
不,您得到的是一个转义的XML片段,表示为字符串。使用禁用输出转义
取消转义的尝试无法工作,因为禁用输出转义
仅在写入输出时执行
如果处理器支持XPath/XSLT 3.0,则可以使用parse-xml-fragment()
函数将字符串转换为节点树:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="elem2" select="parse-xml-fragment(/root/element2/@value)" />
<!-- identity transform -->
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="/">
<root>
<applytemp>
<xsl:apply-templates select="$elem2/name"/>
</applytemp>
<copy-of>
<xsl:copy-of select="$elem2/name"/>
</copy-of>
<value-of>
<xsl:value-of select="$elem2/name"/>
</value-of>
</root>
</xsl:template>
</xsl:stylesheet>
在早期版本中,您需要分两次执行此操作。首先,使用禁用输出转义
取消转义字符串,并将结果保存到文件中。然后处理生成的文件
我有一个XML,其中一些原始值显示为nodeset
不,您得到的是一个转义的XML片段,表示为字符串。使用禁用输出转义
取消转义的尝试无法工作,因为禁用输出转义
仅在写入输出时执行
如果处理器支持XPath/XSLT 3.0,则可以使用parse-xml-fragment()
函数将字符串转换为节点树:
XSLT 3.0
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="elem2" select="parse-xml-fragment(/root/element2/@value)" />
<!-- identity transform -->
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="/">
<root>
<applytemp>
<xsl:apply-templates select="$elem2/name"/>
</applytemp>
<copy-of>
<xsl:copy-of select="$elem2/name"/>
</copy-of>
<value-of>
<xsl:value-of select="$elem2/name"/>
</value-of>
</root>
</xsl:template>
</xsl:stylesheet>
在早期版本中,您需要分两次执行此操作。首先,使用禁用输出转义
取消转义字符串,并将结果保存到文件中。然后处理生成的文件。在写入变量时使用禁用输出转义是一个长期存在的问题和争议的根源,在XSL工作组中被称为“粘性doe”问题。XSLT 1.0规范说明(在§16.4中)
如果结果树片段包含已禁用转义的文本节点,则将结果树片段转换为数字或字符串也是错误的
但勘误表E2说:
如果使用xsl:copy of元素复制根节点(请参见[11.3使用xsl:copy of的变量和参数值]),并且该根节点的文本节点后代禁用了转义,则该文本节点的结果副本也应禁用转义。比如说
在写入变量时使用禁用输出转义应该输出是一个长期存在的问题和争议,在XSL WG中称为“sticky doe”问题。XSLT 1.0规范说明(在§16.4中)
如果结果树片段包含已禁用转义的文本节点,则将结果树片段转换为数字或字符串也是错误的
但勘误表E2说:
如果使用xsl:copy of元素复制根节点(请参见[11.3使用xsl:copy of的变量和参数值]),并且该根节点的文本节点后代禁用了转义,则该文本节点的结果副本也应禁用转义。比如说
应该输出See:See:xslt2.0也在工作。这取决于版本吗?不知道你说的是什么意思parse-xml-fragment()
是一个Xpath 3.0函数。如果它可以工作,那么处理器就支持XSLT 3.0(至少部分支持),XSLT 2.0也可以工作。这取决于版本吗?不知道你说的是什么意思parse-xml-fragment()
是一个Xpath 3.0函数。如果可行,那么处理器支持XSLT 3.0(至少部分支持)。