我有描述檔案的 XML,我嘗試使用 XSLT 將其轉換為分為頁面的 HTML 檔案(每個“eop”標記表示頁面的開頭)。在每個偶數頁上,方向為 rtl,在奇數頁上,方向為 ltr。每個頁面應分為兩部分,標題在一部分,內容在另一部分我嘗試過,但發生的情況是,從“文章”標簽作業正常,但部分“eop”標簽到“文章”標簽仍然存在之前的設定。
有人有解決方案嗎?
xml:
<?xml version="1.0" encoding="UTF-8"?>
<dataRoot >
<eop eId="100"></eop>
<article>
<title>article 1</title>
<content>
<point>
<p>aaa</p>
<p>bbb
<eop eId="101"></eop>
</p>
<p>ccc
</p>
</point>
<point>
<p>ddd</p>
<p>eee</p>
</point>
<p>fff</p>
</content>
</article>
<article>
<title>article 2</title>
<content>
<point>
<p>ggg</p>
<p>hhh
</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" exclude-result-prefixes="xs" version="3.0" >
<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: 10em 5em;
background-color: #ffff;
}
.eop{padding:4em;}
.numPage{padding:2rem 1rem 2rem 2rem;}
</style>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="dataRoot" >
<div class="page">
<div class="content">
<xsl:apply-templates />
</div>
</div>
</xsl:template>
<xsl:template match="eop">
<div class="eop">
-----------------------------------------------------------------------
</div>
<div class="numPage">
number page:
<xsl:value-of select="@eId"/>
</div>
</xsl:template>
<xsl:template match="article" >
<div class="article">
<div>
<xsl:attribute name="class">
<xsl:value-of select="accumulator-before('directionByPage')"/>
</xsl:attribute>
<xsl:apply-templates />
</div>
</div>
</xsl:template>
<xsl:template match="title" >
<div class="title">
<xsl:value-of select="text()"/>
</div>
</xsl:template>
<xsl:template match="content" >
<div class="content">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="point" >
<div class="point">
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="p" >
<div class="p">
<xsl:apply-templates />
</div>
</xsl:template>
</xsl:stylesheet>
輸出html:

我們這里看到方向并沒有從頁面的開頭改變,而只是從文章的開頭改變(因為頁面的名稱被劃分為網格)問題是每個組件是否有可能會尋找'eop' 在它之前(即使它不在 xml 樹中的同一級別)或者有人有另一個解決方案
uj5u.com熱心網友回復:
文本方向不適用于文章級別,但適用于文章中各個元素的級別,因此您需要為所有這些元素指定適當的方向class。
我的建議是添加一個函式,該函式將為每個元素回傳適當的類名,通過查找緊接在前面的元素eop并檢查其編號是奇數還是偶數,然后您可以從每個模板呼叫該函式以生成適當的類。
<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:template match="article" >
<div class="article">
<div class="{text:direction(.)}">
<xsl:apply-templates />
</div>
</div>
</xsl:template>
<xsl:template match="title" >
<div class="title {text:direction(.)}">
<xsl:value-of select="text()"/>
</div>
</xsl:template>
<!-- etc. -->
請注意,函式的名稱有命名空間前綴;這對于樣式表函式是強制性的。因此,您需要xmlns:text="my-namespace-uri"在stylesheet元素上包含該名稱空間的宣告。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489248.html
