당신이 맨 처음 템플릿이 완전한 소스 XML을 통해 소위 "Identity template"의지의 루프 :
<xsl:template match="@*|node()*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
당신이 모든 /Metadata/contact/node/Email
에 일치가 두 번째 템플릿입니다. 나는이 템플릿을 약간 다르게 쓸 것이다. 절대 경로를 일치시키는 대신 노드 Email/CharacterString
을 일치시킨 다음 작업을 수행하는 것이 가장 좋습니다. 템플릿 위
<xsl:template match="Email/CharacterString">
<xsl:copy>
<xsl:choose>
<xsl:when test="contains(ancestor::contact/organisationName/CharacterString,'TestOrg')">
<xsl:value-of select="'[email protected]'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
은 당신이 (Email/CharacterString
인)을 변경하려는 노드와 일치 :
내가 좋아하는 템플릿을 사용할 수 있습니다. 일치하면 xsl:copy
은 현재 선택된 노드 (CharacterString
)를 복사합니다. 값은 ancestor::contact/organisationName/CharacterString
값에 따라 채워집니다. 나는 XPath Axeancestor
을 사용하고 있습니다. 템플릿을 변경할 때 어느 것이 편리할까요?
다음
<xsl:template match="contact[organisationRole/CharacterString = '1']/node/Email/CharacterString">
<xsl:copy>
<xsl:choose>
<xsl:when test="contains(ancestor::contact/organisationName/CharacterString,'TestOrg')">
<xsl:value-of select="'[email protected]'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
난 단지 contact
노드에 템플릿을 적용 : 경우에 변경하려는 연락처 노드를 선택 예를 들어 지금, 템플릿을 변경하려는 경우
, 두 번째 템플릿과 같이 쓸 수있다 여기서 ([]
) organisationRole/CharacterString
은 값 1과 같습니다. 템플리트의 본문은 변경되지 않았습니다. 따라서 XPath Axe의 사용이 유용한 이유를 알 수 있습니다.
전체 XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="contact[organisationRole/CharacterString = '1']/node/Email/CharacterString">
<xsl:copy>
<xsl:choose>
<xsl:when test="contains(ancestor::contact/organisationName/CharacterString,'TestOrg')">
<xsl:value-of select="'[email protected]'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
소스 XML
<?xml version="1.0" encoding="UTF-8"?>
<Metadata>
<contact>
<organisationRole>
<CharacterString>1</CharacterString>
</organisationRole>
<organisationName>
<CharacterString>TestOrg</CharacterString>
</organisationName>
<postalCode>
<CharacterString>2020</CharacterString>
</postalCode>
<node>
<Email>
<CharacterString>[email protected]</CharacterString>
</Email>
</node>
</contact>
<contact>
<organisationRole>
<CharacterString>2</CharacterString>
</organisationRole>
<organisationName>
<CharacterString>Example Org</CharacterString>
</organisationName>
<postalCode>
<CharacterString>8080</CharacterString>
</postalCode>
<node>
<Email>
<CharacterString>[email protected]</CharacterString>
</Email>
</node>
</contact>
<contact>
<organisationRole>
<CharacterString>1</CharacterString>
</organisationRole>
<organisationName>
<CharacterString>Real Org</CharacterString>
</organisationName>
<postalCode>
<CharacterString>9050</CharacterString>
</postalCode>
<node>
<Email>
<CharacterString>[email protected]</CharacterString>
</Email>
</node>
</contact>
</Metadata>
생산 출력
<?xml version="1.0" encoding="UTF-8"?>
<Metadata>
<contact>
<organisationRole>
<CharacterString>1</CharacterString>
</organisationRole>
<organisationName>
<CharacterString>TestOrg</CharacterString>
</organisationName>
<postalCode>
<CharacterString>2020</CharacterString>
</postalCode>
<node>
<Email>
<CharacterString>[email protected]</CharacterString>
</Email>
</node>
</contact>
<contact>
<organisationRole>
<CharacterString>2</CharacterString>
</organisationRole>
<organisationName>
<CharacterString>Example Org</CharacterString>
</organisationName>
<postalCode>
<CharacterString>8080</CharacterString>
</postalCode>
<node>
<Email>
<CharacterString>[email protected]</CharacterString>
</Email>
</node>
</contact>
<contact>
<organisationRole>
<CharacterString>1</CharacterString>
</organisationRole>
<organisationName>
<CharacterString>Real Org</CharacterString>
</organisationName>
<postalCode>
<CharacterString>9050</CharacterString>
</postalCode>
<node>
<Email>
<CharacterString>[email protected]</CharacterString>
</Email>
</node>
</contact>
</Metadata>
편집
경우에만 organisationName/CharacterString
이 TestOrg
XSLT는 다음과 같을 것입니다 텍스트가 들어있는 연락처에서 Email/CharacterString
변경하려면 :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="contact[contains(organisationName/CharacterString,'TestOrg')]/node/Email/CharacterString">
<xsl:copy>
<xsl:value-of select="'[email protected]'"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
주십시오 소스 XML 문서와 요구를 산출. –