Xml 需要使用XSLT正确排列主题元素
我需要使用XSL重新安排topic元素 输入XMLXml 需要使用XSLT正确排列主题元素,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我需要使用XSL重新安排topic元素 输入XML <?xml version="1.0" encoding="UTF-8"?> <Body> <h1>Children</h1> <p> <img src="https://tneb.com/Services/Gets/Contents/ucr-images-v1/Images/cup-36" />This is pain in the stomach
<?xml version="1.0" encoding="UTF-8"?>
<Body>
<h1>Children</h1>
<p>
<img src="https://tneb.com/Services/Gets/Contents/ucr-images-v1/Images/cup-36" />This is pain in the stomach or belly.</p>
<h2>Causes</h2>
<p>When you put a load balancer</p>
<h3>Found</h3>
<p>balancer</p>
<h4>Height</h4>
<p>stomach</p>
</Body>
儿童
这是胃或腹部疼痛
原因
当您放置负载平衡器时
建立
平衡器
高度
胃
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all">
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<xsl:choose>
<xsl:when test="self::h3">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<xsl:choose>
<xsl:when test="self::h4">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="img">
<xsl:element name="image">
<xsl:attribute name="id">
<xsl:value-of select="@imageid"/>
</xsl:attribute>
<xsl:attribute name="alt">
<xsl:value-of select="@alt"/>
</xsl:attribute>
<xsl:attribute name="height">
<xsl:value-of select="@height"/>
</xsl:attribute>
<xsl:attribute name="width">
<xsl:value-of select="@width"/>
</xsl:attribute>
<xsl:attribute name="align">
<xsl:value-of select="@class"/>
</xsl:attribute>
<xsl:attribute name="href">
<xsl:value-of select="tokenize(@src, '/')[position() gt last() - 3]" separator="/"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
</xsl:stylesheet>
话题_
话题_
话题_
话题_
输出
<topic id="topic_1">
<title>Children</title>
<topic id="topic_">
<title>
<image id="47"
alt="cup."
height="300"
width="400"
align="right"
href="https://tneb.com/Services/Gets/Contents/ucr-images-v1/Images/cup-36"/>
This is pain in the stomach or belly.</title>
</topic>
<topic id="topic_2">
<title>Causes</title>
<p>When you put a load balancer</p>
</topic>
<topic id="topic_3">
<title>Found</title>
<p>balancer</p>
</topic>
<topic id="topic_4">
<title>Height</title>
<p>stomach</p>
</topic>
</topic>
<topic id="topic_1">
<title>Children</title>
<body>
<image id="47"
alt="cup."
height="300"
width="400"
align="right"
href="/ucr-images-v1/Images/cup-36"/>
<p>This is pain in the stomach or belly.</p>
</body>
<topic id="topic_2">
<title>Causes</title>
<p>When you put a load balancer</p>
</topic>
<topic id="topic_3">
<title>Found</title>
<p>balancer</p>
</topic>
<topic id="topic_4">
<title>Height</title>
<p>stomach</p>
</topic>
</topic>
儿童
这是胃或腹部疼痛。
原因
当您放置负载平衡器时
建立
平衡器
高度
胃
预期产出
<topic id="topic_1">
<title>Children</title>
<topic id="topic_">
<title>
<image id="47"
alt="cup."
height="300"
width="400"
align="right"
href="https://tneb.com/Services/Gets/Contents/ucr-images-v1/Images/cup-36"/>
This is pain in the stomach or belly.</title>
</topic>
<topic id="topic_2">
<title>Causes</title>
<p>When you put a load balancer</p>
</topic>
<topic id="topic_3">
<title>Found</title>
<p>balancer</p>
</topic>
<topic id="topic_4">
<title>Height</title>
<p>stomach</p>
</topic>
</topic>
<topic id="topic_1">
<title>Children</title>
<body>
<image id="47"
alt="cup."
height="300"
width="400"
align="right"
href="/ucr-images-v1/Images/cup-36"/>
<p>This is pain in the stomach or belly.</p>
</body>
<topic id="topic_2">
<title>Causes</title>
<p>When you put a load balancer</p>
</topic>
<topic id="topic_3">
<title>Found</title>
<p>balancer</p>
</topic>
<topic id="topic_4">
<title>Height</title>
<p>stomach</p>
</topic>
</topic>
儿童
这是胃或腹部疼痛
原因
当您放置负载平衡器时
建立
平衡器
高度
胃
我需要删除没有Id号的主题,我需要
元素,而不是像上面那样的
元素 您还必须添加与h2中的h3相同的条件
<xsl:choose>
<xsl:when test="self::h2">
..........
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
..........