我想將 xml 分成頁面,只要有<eop>標簽,就會開始一個新頁面。我有呼叫特定元素,我看到它破壞了劃分.. 當我放入組時: <xsl:apply-templates select="current-group()"/>
那很好。而且因為我改成了一個特定的電話: <xsl:apply-templates select="current-group()/self::title"/> <xsl:apply-templates select="current-group()/self::article"它被摧毀了..
有人知道如何在群組中撥打特定電話嗎?
xml代碼:
<?xml version="1.0" encoding="UTF-8"?>
<dataRoot >
<eop eId="100"></eop>
<article>
<content>
<p>page 100 article 1 p 1</p>
</content>
<title>article 1</title>
<content>
<point>
<p>page 100 article 1 p 2</p>
<p>page 100 article 1 p 3
<p>page 100 article 1 p 4
<eop eId="101"></eop>
page 101 article 1 p 5
</p>
<p>page 101 article 1 p 6</p>
</p>
<p>page 101 article 1 p 7
</p>
</point>
<point>
<p>page 101 article 1 p 8</p>
</point>
</content>
</article>
<article>
<title>article 2</title>
<content>
<point>
<p>page 101 article 2 p 1</p>
<p>page 101 article 2 p 2
</p>
</point>
</content>
</article>
</dataRoot>
xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:text="my-namespace-uri" exclude-result-prefixes="xs" version="3.0" >
<xsl:function name="text:direction">
<xsl:param name="element"/>
<xsl:variable name="eId" select="$element/preceding-sibling::eop[1]/@eId"/>
<xsl:sequence select="
if (number(translate($eId, translate($eId, '0123456789', ''), '')) mod 2 = 0) then
'dir_rtl'
else
'dir_ltr'
"/>
</xsl:function>
<xsl:mode use-accumulators="#all" streamable="no"/>
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:accumulator name="directionByPage" as="xs:string?" initial-value="'dir_rtl'">
<xsl:accumulator-rule match="eop" select="if (number(translate(@eId, translate(@eId, '0123456789', ''), '')) mod 2 = 0) then 'dir_rtl' else 'dir_ltr'"/>
</xsl:accumulator>
<xsl:template match="/">
<html>
<head>
<style type="text/css">
.dir_rtl{ text-align: right; display: grid; direction: rtl; grid-template-columns: 20% 80%; }
.dir_ltr{ text-align: right; display: grid; direction: ltr; grid-template-columns: 15% 85%; }
.page{
margin: 7em;
background-color: rgb(68,68,68); /* Needed for IEs */
-moz-box-shadow: 5px 5px 5px rgba(68,68,68,0.6);
-webkit-box-shadow: 5px 5px 5px rgba(68,68,68,0.6);
box-shadow: 0px 1px 5px rgb(68 68 68 / 60%);
filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30);
-ms-filter: "progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30)";
zoom: 1;
}
.page .content {
position: relative; /* This protects the inner element from being blurred */
padding: 1em 2em;
background-color: #ffff;
}
.eop{padding:2em;}
.numPage{padding:1rem 1rem 1rem 2rem;}
</style>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="dataRoot">
<xsl:for-each-group select=".//node()" group-starting-with="eop">
<div class="page">
<div class="content">
<xsl:apply-templates select="current-group()/self::title"/>
<xsl:apply-templates select="current-group()/self::article"/>
</div>
</div>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="eop">
<div class="numPage">
number page:
<xsl:value-of select="descendant-or-self::node()/@eId"/>
</div>
</xsl:template>
<xsl:template match="article" >
<div class="article">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="title">
<xsl:value-of select="descendant-or-self::node()/text()"/>
</xsl:template>
<xsl:template match="content" >
<div class="content">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="point">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="p" >
<div class="p">
<xsl:value-of select="descendant-or-self::node()/text()"/>
</div>
</xsl:template>
</xsl:stylesheet>
結果:

該頁面應以 eop 101 結尾,而應以第 1 條結尾
uj5u.com熱心網友回復:
也許使用兩種不同的模式,您可以在其中處理所有元素,但title一種模式會輸出當前組的層次結構,但會阻止title和其他輸出,例如title:
<xsl:template match="dataRoot">
<xsl:variable name="parent" select="."/>
<xsl:for-each-group select="descendant::node()" group-starting-with="eop">
<div class="page">
<div class="content">
<xsl:apply-templates select="current-group()[self::title]"/>
<xsl:apply-templates select="$parent/node()[descendant-or-self::node() intersect current-group()]" mode="subtree"/>
</div>
</div>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="eop" mode="subtree"/>
<xsl:template match="node()" mode="subtree">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()[descendant-or-self::node() intersect current-group()]" mode="subtree"/>
</xsl:copy>
</xsl:template>
<xsl:template match="article/title" mode="subtree"/>
<xsl:template match="article/title">
<h2>
<xsl:apply-templates/>
</h2>
</xsl:template>
可能映射article/title到一個h2元素不是你想要的,但當然你可以讓模板匹配article/title創建你想要的任何輸出,所以讓上面的特定示例不要讓你感到困惑,我只需要一些方法來區分“特殊”處理title在組的其余部分處理的輸出中。
至于您的評論,您自己的模板將被改編,例如
<xsl:template match="content" >
<div class="content">
<xsl:apply-templates />
</div>
</xsl:template>
會成為
<xsl:template match="content" mode="subtree">
<div class="content">
<xsl:apply-templates select="node()[descendant-or-self::node() intersect current-group()]" mode="subtree"/>
</div>
</xsl:template>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/493452.html
上一篇:如何在XSL模板的XPath運算式中查找所有不匹配的元素?
下一篇:C#XML-獲取可選標簽?
