Xml 使用文本生成缩进属性值
我需要将一个输入xml转换成另一个xml,它应该打印每个元素,如果元素具有特定属性(x或y),它应该打印它的值和内联文本。输入的每个元素都应该发生。根据每个元素的层次结构,它应该添加缩进Xml 使用文本生成缩进属性值,xml,xslt,xslt-1.0,xslt-2.0,Xml,Xslt,Xslt 1.0,Xslt 2.0,我需要将一个输入xml转换成另一个xml,它应该打印每个元素,如果元素具有特定属性(x或y),它应该打印它的值和内联文本。输入的每个元素都应该发生。根据每个元素的层次结构,它应该添加缩进 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:template match="/"> &
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:element name="newXML">
<xsl:for-each select="root//*">
<p>
<xsl:text>{</xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>}</xsl:text>
</p>
<xsl:if test="@x or @y">
<p><xsl:text>[Start attribute x=</xsl:text><xsl:value-of select="@x"/><xsl:text>]</xsl:text></p>
<p><xsl:text>[Start attribute y=</xsl:text><xsl:value-of select="@y"/><xsl:text>]</xsl:text></p>
</xsl:if>
<xsl:choose>
<xsl:when test="text()[1][(string-length(normalize-space(.)) = 0)]">
<p></p>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:apply-templates/>
</p>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="@x or @y">
<p><xsl:text>[end attribute x=</xsl:text><xsl:value-of select="@x"/><xsl:text>]</xsl:text></p>
<p><xsl:text>[end attribute y=</xsl:text><xsl:value-of select="@y"/><xsl:text>]</xsl:text></p>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="self::ol and parent::para"></xsl:when>
<xsl:otherwise><xsl:apply-templates></xsl:apply-templates></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
输入XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<tom x="1" y="2" z='11'>
<para x="3" y="4" z='22'>This is first para</para>
<para x="5" y="6" z='23'>This is seond para
<ol x="7">
<li x="8" y="9" z='24'>this is listitem</li>
<li x="9" y="10" z='25'>this is listitem</li>
</ol>
</para>
</tom>
<harry x='11' y='12'>
<para x='13' y='14'>This is third para <b x='13'>this is bold <i y='14'>this is italic uder bold</i></b></para>
</harry>
</root>
这是第一段
这是第二段
这是listitem
这是列表项
这是第三段这是粗体这是斜体乌德粗体
应该把它放出来
<newXML>
<p>{root}</p>
<p> {tom}</p>
<p> [Start attribute x='1']</p>
<p> [Start attribute y='2']</p>
<p> {para}</p>
<p> [Start attribute x='3']</p>
<p> [Start attribute y='4']</p>
<p> This is first para</p>
<p> [end attribute x='3']</p>
<p> [end attribute y='4']</p>
<p> {para}</p>
<p> [Start attribute x='5']</p>
<p> [Start attribute y='6']</p>
<p> This is second para</p>
<p> {ol}</p>
<p> [Start attribute x='7']</p>
<p> {li}</p>
<p> [Start attribute x='8']</p>
<p> [Start attribute y='9']</p>
<p> this is listitem</p>
<p> [end attribute x='8']</p>
<p> [end attribute y='9']</p>
<p> {li}</p>
<p> [Start attribute x='9']</p>
<p> [Start attribute y='10']</p>
<p> this is listitem</p>
<p> [end attribute x='9']</p>
<p> [end attribute y='10']</p>
<p> [end attribute x='7']</p>
<p> [end attribute x='5']</p>
<p> [end attribute y='6']</p>
<p> [end attribute x='1']</p>
<p> [end attribute y='2']</p>
<p> {harry}</p>
<p> ....</p>
</newXML>
{root}
{tom}
[开始属性x='1']
[开始属性y='2']
{para}
[开始属性x='3']
[开始属性y='4']
这是第一段
[结束属性x='3']
[结束属性y='4']
{para}
[开始属性x='5']
[开始属性y='6']
这是第二段
{ol}
[开始属性x='7']
{li}
[开始属性x='8']
[开始属性y='9']
这是列表项
[结束属性x='8']
[结束属性y='9']
{li}
[开始属性x='9']
[开始属性y='10']
这是列表项
[结束属性x='9']
[结束属性y='10']
[结束属性x='7']
[结束属性x='5']
[结束属性y='6']
[结束属性x='1']
[结束属性y='2']
{哈利}
我正在尝试类似这样的方法,对于像‘ol’、‘b’、‘I’这样的内联元素,它失败了。也不能放缩进
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="/">
<xsl:element name="newXML">
<xsl:for-each select="root//*">
<p>
<xsl:text>{</xsl:text><xsl:value-of select="local-name(.)"/><xsl:text>}</xsl:text>
</p>
<xsl:if test="@x or @y">
<p><xsl:text>[Start attribute x=</xsl:text><xsl:value-of select="@x"/><xsl:text>]</xsl:text></p>
<p><xsl:text>[Start attribute y=</xsl:text><xsl:value-of select="@y"/><xsl:text>]</xsl:text></p>
</xsl:if>
<xsl:choose>
<xsl:when test="text()[1][(string-length(normalize-space(.)) = 0)]">
<p></p>
</xsl:when>
<xsl:otherwise>
<p>
<xsl:apply-templates/>
</p>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="@x or @y">
<p><xsl:text>[end attribute x=</xsl:text><xsl:value-of select="@x"/><xsl:text>]</xsl:text></p>
<p><xsl:text>[end attribute y=</xsl:text><xsl:value-of select="@y"/><xsl:text>]</xsl:text></p>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="self::ol and parent::para"></xsl:when>
<xsl:otherwise><xsl:apply-templates></xsl:apply-templates></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
{}
[开始属性x=]
[开始属性y=]
[结束属性x=]
[结束属性y=]
Phew的确如此。我暂时将缩进问题放在一边,请您检查以下样式表:
<?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" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/root">
<newXML>
<xsl:apply-templates select="*"/>
</newXML>
</xsl:template>
<xsl:template match="*">
<p>
<xsl:text>{</xsl:text>
<xsl:value-of select="local-name(.)"/>
<xsl:text>}</xsl:text>
</p>
<xsl:if test="@x">
<p>
<xsl:text>[Start attribute x=</xsl:text>
<xsl:value-of select="@x"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
<xsl:if test="@y">
<p>
<xsl:text>[Start attribute y=</xsl:text>
<xsl:value-of select="@y"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
<xsl:variable name="mytext" select="normalize-space(./text())" />
<xsl:if test="$mytext">
<p>
<xsl:value-of select="$mytext"/>
</p>
</xsl:if>
<xsl:apply-templates select="*"/>
<xsl:if test="@x">
<p>
<xsl:text>[end attribute x=</xsl:text>
<xsl:value-of select="@x"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
<xsl:if test="@y">
<p>
<xsl:text>[end attribute y=</xsl:text>
<xsl:value-of select="@y"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
{
}
[开始属性x=
]
[开始属性y=
]
[结束属性x=
]
[结束属性y=
]
使用示例输入,将生成以下输出:
<?xml version="1.0" encoding="utf-8"?>
<newXML>
<p>{tom}</p>
<p>[Start attribute x=1]</p>
<p>[Start attribute y=2]</p>
<p>{para}</p>
<p>[Start attribute x=3]</p>
<p>[Start attribute y=4]</p>
<p>This is first para</p>
<p>[end attribute x=3]</p>
<p>[end attribute y=4]</p>
<p>{para}</p>
<p>[Start attribute x=5]</p>
<p>[Start attribute y=6]</p>
<p>This is seond para</p>
<p>{ol}</p>
<p>[Start attribute x=7]</p>
<p>{li}</p>
<p>[Start attribute x=8]</p>
<p>[Start attribute y=9]</p>
<p>this is listitem</p>
<p>[end attribute x=8]</p>
<p>[end attribute y=9]</p>
<p>{li}</p>
<p>[Start attribute x=9]</p>
<p>[Start attribute y=10]</p>
<p>this is listitem</p>
<p>[end attribute x=9]</p>
<p>[end attribute y=10]</p>
<p>[end attribute x=7]</p>
<p>[end attribute x=5]</p>
<p>[end attribute y=6]</p>
<p>[end attribute x=1]</p>
<p>[end attribute y=2]</p>
<p>{harry}</p>
<p>[Start attribute x=11]</p>
<p>[Start attribute y=12]</p>
<p>{para}</p>
<p>[Start attribute x=13]</p>
<p>[Start attribute y=14]</p>
<p>This is third para</p>
<p>{b}</p>
<p>[Start attribute x=13]</p>
<p>this is bold</p>
<p>{i}</p>
<p>[Start attribute y=14]</p>
<p>this is italic uder bold</p>
<p>[end attribute y=14]</p>
<p>[end attribute x=13]</p>
<p>[end attribute x=13]</p>
<p>[end attribute y=14]</p>
<p>[end attribute x=11]</p>
<p>[end attribute y=12]</p>
</newXML>
{tom}
[开始属性x=1]
[开始属性y=2]
{para}
[开始属性x=3]
[开始属性y=4]
这是第一段
[结束属性x=3]
[结束属性y=4]
{para}
[开始属性x=5]
[开始属性y=6]
这是第二段
{ol}
[开始属性x=7]
{li}
[开始属性x=8]
[开始属性y=9]
这是列表项
[结束属性x=8]
[结束属性y=9]
{li}
[开始属性x=9]
[开始属性y=10]
这是列表项
[结束属性x=9]
[结束属性y=10]
[结束属性x=7]
[结束属性x=5]
[结束属性y=6]
[结束属性x=1]
[结束属性y=2]
{哈利}
[开始属性x=11]
[开始属性y=12]
{para}
[开始属性x=13]
[开始属性y=14]
这是第三段
{b}
[开始属性x=13]
这是大胆的
{i}
[开始属性y=14]
这是斜体加粗的
[结束属性y=14]
[结束属性x=13]
[结束属性x=13]
[结束属性y=14]
[结束属性x=11]
[结束属性y=12]
如果这是令人满意的,我们可以在下一步讨论压痕。Phew确实如此。我暂时将缩进问题放在一边,请您检查以下样式表:
<?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" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/root">
<newXML>
<xsl:apply-templates select="*"/>
</newXML>
</xsl:template>
<xsl:template match="*">
<p>
<xsl:text>{</xsl:text>
<xsl:value-of select="local-name(.)"/>
<xsl:text>}</xsl:text>
</p>
<xsl:if test="@x">
<p>
<xsl:text>[Start attribute x=</xsl:text>
<xsl:value-of select="@x"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
<xsl:if test="@y">
<p>
<xsl:text>[Start attribute y=</xsl:text>
<xsl:value-of select="@y"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
<xsl:variable name="mytext" select="normalize-space(./text())" />
<xsl:if test="$mytext">
<p>
<xsl:value-of select="$mytext"/>
</p>
</xsl:if>
<xsl:apply-templates select="*"/>
<xsl:if test="@x">
<p>
<xsl:text>[end attribute x=</xsl:text>
<xsl:value-of select="@x"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
<xsl:if test="@y">
<p>
<xsl:text>[end attribute y=</xsl:text>
<xsl:value-of select="@y"/>
<xsl:text>]</xsl:text>
</p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
{
}
[开始属性x=
]
[开始属性y=
]
<root>
<tom x="1" y="2" z='11'>
<para x="3" y="4" z='22'>This is first para</para>
<para x="5" y="6" z='23'>This is seond para
<ol x="7">
<li x="8" y="9" z='24'>this is listitem</li>
<li x="9" y="10" z='25'>this is listitem</li>
</ol>
</para>
</tom>
<harry x='11' y='12'>
<para x='13' y='14'>This is third para <b x='13'>this is bold <i y='14'>this is italic uder bold</i></b></para>
</harry>
</root>
<xsl:stylesheet version="2.0" xmlns:l="local" xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="l xs" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="include-attrs" select="('x','y')"/>
<xsl:function name="l:indent">
<xsl:param name="level"/>
<xsl:for-each select="1 to $level">
<xsl:text>    </xsl:text>
</xsl:for-each>
</xsl:function>
<xsl:template match="/">
<newXML>
<xsl:apply-templates/>
</newXML>
</xsl:template>
<xsl:template match="*">
<xsl:variable name="level" select="count(ancestor-or-self::*)-1"/>
<p>
<xsl:value-of select="l:indent($level)"/>
<xsl:value-of select="concat('{',local-name(),'}')"/>
</p>
<xsl:apply-templates select="@*[local-name()=$include-attrs]">
<xsl:with-param name="mode" select="'Start'"/>
<xsl:with-param name="level" select="$level"/>
</xsl:apply-templates>
<xsl:apply-templates/>
<xsl:apply-templates select="@*[local-name()=$include-attrs]">
<xsl:with-param name="mode" select="'End'"/>
<xsl:with-param name="level" select="$level"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="@*">
<xsl:param name="mode"/>
<xsl:param name="level"/>
<p>
<xsl:value-of select="l:indent($level)"/>
<xsl:value-of select="concat('[',$mode,' attribute ',local-name(),'=''',.,'''',']')"/>
</p>
</xsl:template>
<xsl:template match="text()">
<xsl:variable name="level" select="count(ancestor-or-self::*)"/>
<p>
<xsl:value-of select="l:indent($level)"/>
<xsl:value-of select="normalize-space(.)"/>
</p>
</xsl:template>
</xsl:stylesheet>
<newXML>
<p>{root}</p>
<p> {tom}</p>
<p> [Start attribute x='1']</p>
<p> [Start attribute y='2']</p>
<p> {para}</p>
<p> [Start attribute x='3']</p>
<p> [Start attribute y='4']</p>
<p> This is first para</p>
<p> [End attribute x='3']</p>
<p> [End attribute y='4']</p>
<p> {para}</p>
<p> [Start attribute x='5']</p>
<p> [Start attribute y='6']</p>
<p> This is seond para</p>
<p> {ol}</p>
<p> [Start attribute x='7']</p>
<p> {li}</p>
<p> [Start attribute x='8']</p>
<p> [Start attribute y='9']</p>
<p> this is listitem</p>
<p> [End attribute x='8']</p>
<p> [End attribute y='9']</p>
<p> {li}</p>
<p> [Start attribute x='9']</p>
<p> [Start attribute y='10']</p>
<p> this is listitem</p>
<p> [End attribute x='9']</p>
<p> [End attribute y='10']</p>
<p> [End attribute x='7']</p>
<p> [End attribute x='5']</p>
<p> [End attribute y='6']</p>
<p> [End attribute x='1']</p>
<p> [End attribute y='2']</p>
<p> {harry}</p>
<p> [Start attribute x='11']</p>
<p> [Start attribute y='12']</p>
<p> {para}</p>
<p> [Start attribute x='13']</p>
<p> [Start attribute y='14']</p>
<p> This is third para</p>
<p> {b}</p>
<p> [Start attribute x='13']</p>
<p> this is bold</p>
<p> {i}</p>
<p> [Start attribute y='14']</p>
<p> this is italic uder bold</p>
<p> [End attribute y='14']</p>
<p> [End attribute x='13']</p>
<p> [End attribute x='13']</p>
<p> [End attribute y='14']</p>
<p> [End attribute x='11']</p>
<p> [End attribute y='12']</p>
</newXML>