이제 xsl을 여러 번 변경했지만 올바른 방법을 찾지 못했습니다. 이제 내가 원하는 것은 다음과 같습니다.
이제 설명이없는 모든 누락 된 페이지를 찾으려고합니다. 누락 된 경우 해당 페이지에 대한 설명을 추가하고, 존재하는 경우 설명을 수정하고 싶습니다. pageX에서 pageXDescription까지의 문자열은 항상 동일합니다.
다음은 짧은 예제 xml입니다.
<BOOK>
<PAGE NAME='page1' VALUE='coolText'/>
<PAGE NAME='Description1' VALUE='coolDescription'/>
<PAGE NAME='page2' VALUE='moreText'/>
<PAGE NAME='page3' VALUE='aLotMoreText'/>
<PAGE NAME='Description3' VALUE='aLotMoreDescriptions'/>
</BOOK>
나는 다음과 같이 그것을 시도했다.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- define output settings and header -->
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" media-type="string" encoding="ISO-8859-1" doctype-system="deftable.dtd"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page1']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page1'"/>
<xsl:with-param name="description" select="'Description1'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page2']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page2'"/>
<xsl:with-param name="description" select="'Description2'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page3']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page3'"/>
<xsl:with-param name="description" select="'Description3'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<!-- Function to generate missing XML Tags -->
<xsl:template name="create_missing_description_pages">
<xsl:param name="page"/>
<xsl:param name="description"/>
<xsl:param name="new_description"/>
<xsl:apply-templates select="@*|VARIABLE[@NAME=$page]/preceding-sibling::node()"/>
<xsl:apply-templates select="VARIABLE[@NAME=$page]"/>
<xsl:if test="not(VARIABLE/@NAME=$description)">
<xsl:element name="PAGE">
<xsl:attribute name="NAME"><xsl:value-of select="$description"/></xsl:attribute>
<xsl:attribute name="VALUE"><xsl:value-of select="$new_description"/></xsl:attribute>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="VARIABLE[@NAME=$page]/following-sibling::node()"/>
</xsl:template>
<xsl:template match="BOOK/PAGE">
<xsl:copy>
<xsl:choose>
<xsl:when test="@NAME='Description1'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<xsl:when test="@NAME='Description2'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<xsl:when test="@NAME='page3Description'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<!-- other child items will just be copied -->
<xsl:otherwise>
<xsl:copy-of select="@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
모든 설명이 누락되거나 모든 설명을 사용할 수있는 경우 모든 경우에 예상되는 것입니다.
<BOOK>
<PAGE NAME='page1' VALUE='coolText'/>
<PAGE NAME='Description1' VALUE='newContent'/>
<PAGE NAME='page2' VALUE='moreText'/>
<PAGE NAME='Description2' VALUE='newContent'/>
<PAGE NAME='page3' VALUE='aLotMoreText'/>
<PAGE NAME='Description3' VALUE='newContent'/>
</BOOK>
page3이 없으면 페이지 설명도 필요하지 않습니다.
이해할 수 있기를 바랍니다.
내 논리적 실패의 위치와 해결 방법에 대한 힌트를 주셔서 감사합니다.
안부 Björn
이것을 간단하게 만들 수 없습니까?
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PAGE">
<xsl:variable name="pagenum" select="xs:integer(substring-after(@NAME, 'page'))" />
<xsl:copy-of select="."/>
<PAGE NAME='Description{$pagenum}'>
<xsl:attribute name="VALUE">
<xsl:choose>
<xsl:when test="$pagenum=1">newContent1</xsl:when>
<xsl:when test="$pagenum=2">newContent2</xsl:when>
<xsl:when test="$pagenum=3">newContent3</xsl:when>
</xsl:choose>
</xsl:attribute>
</PAGE>
</xsl:template>
<xsl:template match="PAGE[starts-with(@NAME, 'Description')]"/>
</xsl:stylesheet>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다